一、 什么是JDBC
JDBC(Java Database Connectivity,Java数据库连接)是一种用于执行SQL语句的JavaAPI,可以为多种关系型数据库(如Oracle、Sybase、SQL Server、Access等)提供统一访问接口,它由一组Java语言编写的类和接口组成,使数据库开发人员能够用标准JavaAPI编写数据库应用程序。
原理:
二、 JDBC常用类
DriverManager 类是 JDBC 的管理层,作用于用户和驱动程序之间。 它跟踪可用的驱动程序,并在数据库和相应驱动程序之间建立连接。 另外,DriverManager 类也处理诸如驱动程序登录时间限制及登录和 跟踪消息的显示等事务。 对于简单的应用程序,一般程序员需要在此类中直接使用的唯一方法是 DriverManager.getConnection()用于建立数据库的连接。
1. DriverManager类:
负责管理JDBC驱动程序。使用JDBC驱动程序之前,必须先将驱动程序加载并向DriverManager注册后才可以使用,同时提供方法来建立与数据库的连接。
方法:
A、Class.forName(String driver).newInstance(); //加载注册驱动程序
B、Static Connection getConnection(String url,String user,String password) throws SQLException; //取得对数据库的连接
C、Static Driver getDriver(String url) throws SQLExcetion;
//在已经向DriverManager注册的驱动程序中寻找一个能够打开url所指定的数据库的驱动程序 2、 Connection类
负责维护JSP/JAVA数据库程序和数据库之间的联机。可以建立三个非常有用的类对象。
方法:
A、Statement createStatement() throws SQLException; //建立Statement类对象
Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException; // 建立Statement类对象
B、DatabaseMetaData getMetaData() throws SQLException;//建立DatabaseMetaData类对象
3、Statement类
通过Statement类所提供的方法,可以利用标准的SQL命令,对数据库直接新增、删除或修改操作
方法:
A、ResultSet executeQuery(String sql) throws SQLException //使用SELECT命令对数据库进行查询
B、int executeUpdate(String sql) throws SQLException //使用INSERT\DELETE\UPDATE对数据库进行新增、删除和修改操作。
C、void close() throws SQLException //结束Statement类对象对数据库的联机
三、 Java连接数据库步骤(查询为例)
(1)加载连接数据库的驱动程序 Class.forName("com.mysql.jdbc.Driver").newInstance();
(2)创建与数据源的连接
String url="jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=gbk";
String user="root";
String password="";
Connection con=DriverManager.getConnection(url,user,password);
(3)查询数据库:创建Statement对象并执行SQL语句以返回一个ResultSet对象。
String sql="select * from DBTableName";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
(4)获得当前记录集中的某一记录的各个字段的值
String name=rs.getString("Name");
int age=rs.getInt("age");
float wage=rs.getFloat("wage");
(5)关闭查询语句及与数据库的连接(注意关闭的顺序先rs再stmt最后为con)
rs.close();
stmt.close();
con.close();
四、 不同数据库的连接
1、 Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
2、 DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
3、 Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
4、 Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、 MySQL数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/ mydb? useUnicode=true&characterEncoding=gbk";
6、 PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
7、 ODBC
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );