在java调用不同的数据库最基本的区别就是驱动不同,JDBC全称是:Java DataBase Connectivity standard,相信你翻译就明白大概的意思。分为6个步骤 1. load the driver (1)Class.forName()|Class.forName().newlnstance()|new DriverName() (2)实例化时自动向DriverManager注册,不需要显示调用DriverManager.registerDriver 2. Connect to the DataBase DriverManager.getConnection() 3. Excute the SQL (1)connection.CreateStatement() (2)Statement.excuteQuery() (3)Statement.executeUpdate() 4. Retrieve the result data 循环取得结果 while(rs.next()) 5. show the result data 将数据库中的各种类型转换为java中的类型(getXXX)方法 6. close close the resultset / close the statement /close the connection 再给个链接mysql例子 package db;
import java.sql.*;
class Jdbc3 { public static void main(String[] args) { try {
JDBC不关心实际数据库的类型差异,只尝试可能的,符合SQL标准的数据类型转换
相应数据库的转换规则请参考各厂商给出的DOC,以及说明文件Hibernate用方言(Dialect)来构造不同的SQL语句,这其实是一种代理模式的应用
1.
load the driver
(1)Class.forName()|Class.forName().newlnstance()|new DriverName()
(2)实例化时自动向DriverManager注册,不需要显示调用DriverManager.registerDriver
2.
Connect to the DataBase
DriverManager.getConnection()
3.
Excute the SQL
(1)connection.CreateStatement()
(2)Statement.excuteQuery()
(3)Statement.executeUpdate()
4.
Retrieve the result data
循环取得结果 while(rs.next())
5.
show the result data
将数据库中的各种类型转换为java中的类型(getXXX)方法
6.
close
close the resultset / close the statement /close the connection 再给个链接mysql例子
package db;
import java.sql.*;
class Jdbc3 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "1234");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test.admin");
while (rs.next()) {
System.out.println(rs.getString("username"));
System.out.println(rs.getInt("id"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();}
}
finally{
rs.close();
stmt.close();
conn.close();
}
}
也就是说JDBC面向标准SQL类型工作,标准SQL类型和具体数据库的类型转换由厂商规定?理解的对么。。大侠一定要给我个正面的、直接的回复,我纠结好久了但是Hibernate的那个还是没想明白。。
JDBC不代表任何数据库,他是一组访问数据库的接口
各厂商推出的JDBC驱动实现,只是实现了这组接口而你调用JDBC的接口,也不需要关心底层驱动是怎样处理的,你只要符合T-SQL就好了啊比如SQLSERVER的SQLJDBC4 DRIVER,他自然会把数据库的integer对象转成int,varchar等转成String
MYSQL的驱动,把无符号int转成long,有符号int转成Integer
具体的规则,看说明文件撒...
我去看看Hibernate的类型是个什么意思。。
hibernate是封装过的,是可以跨数据库的,配置映射文件时,用hibernate的数据类型,比如string,那么它会按照所定义数据库言语自己去适应不同数据库了,这个我们就不用管了
就是说设置好hibernate和数据库,中间过程就不管了是么。。?