| DataBase | Driver Class |
|---|---|
| MySQL | com.mysql.jdbc.Driver |
| Oracle | oracle.jdbc.driver.OracleDriver |
| SQL Server | com.microsoft.sqlserver.jdbc.SQLServerDriver |
| PostgreSQL | org.postgresql.Driver |
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDrive());
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive");
System.setProperty("jdbc.drivers","DRIVER");
System.setProperty("jdbc.drivers","Driver1:Driver2");
| DriverManager中的常用方法 | 描述 |
|---|---|
| static void registerDriver(new Driver()) | 注册一个数据库驱动 |
| static void deregisterDriver(Driver driver) | 从驱动列表中删除给定的数据库驱动 |
| static Driver getDriver(String URL) | 获取用URL指定的数据库驱动 |
| static Connection getConnection(String JDBCUrl);static Connection getConnection(String JDBCUrl, Properties info); static Connection getConnection(String JDBCUrl, String username, String password) | 获取与数据库的连接,使用1-3个参数:JDBCUrl: 为数据源URL; info: 数据库属性; username: 用户名; password: 密码; |
Connection conn=DriverManager.getConnection(url,"id","psd");
String url_ms="jdbc:sqlserver://127.0.8.88:1433:DatabaseName=master";
String url_mysql="jdbc:mysql://10.0.7.76:3304/myDB";
String user="aName";
String psd="123456";
Connection con=DriverManager.getConnection(url,user,psd);
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/EmployeeDB");
Connection conn=ds.getConnection("myPassword", "myUserName");
try{
Connection conn=DriverManager.getConnection("jdbc:odbc:sun","zhang","abcde");
}catch(SQLException e){
//...
}
| Conneciton接口常用方法 | 说明 |
|---|---|
| Statement createStatement() | 创建一个Statement实例 |
| Statement createStatement(int resultSetType, int resultSetConcurrency) | resultSetType: 实例类型,resultSetConcurrenty: 并发性的结果集 |
| PreparedStatement preparedStatement(String sql) | 创建一个PreparedStatement实例 |
| PreparedStatement preparedStatement(String sql, int resultSetType, int resultSetConcurrency) | sql:数据库SQL语句,resultSetType:类型 |
创建SQL工作空间,实质是创建Statement实例,利用Connection的createStatement()方法
try{
Statement stt=conn.createStatement();
}catch(SQLException e){
//...
}
try{
ResultSet resultSet=stt.executeQuery("SELECT * from emp where empno = * FROM 员工表 WHERE 员工年薪 >= 55" );
}catch(SQLException e){
//...
}
String sqlQuery = "SELECT PRODUCT FROM SUPPLIERTABLE WHERE PRODUCT = 'Bolts'";
| Statement接口常用方法 | 说明 |
|---|---|
| void close() | 关闭当前的Statement实例 |
| void cancel() | 取消Statement实例中的SQL数据库操作命令 |
| ResultSet executeQuery(String sql) | 执行SQL Select语句,将查询结果存放在一个ResultSet对象中 |
| void executeUpdate(String sql) | 执行SQL更新语句(update,delete,insert),返回整数表示所影响的数据库表行数 |
| boolean execute(String sql) | 执行(返回多相结果集)SQL语句,即前两个方法合并 |
| int[] executeBatch() | 在Statement对象中建立批执行SQL语句表 |
| void addBatch(String sql) | 向批执行表中添加SQL语句 |
| void clearBatch() | 清除在Statement对象中建立的批执行SQL语句 |
| ResultSet getResultSet() | 返回当前结果集 |
| boolean getMoreResults() | 移动到Statement实例的下一个结果集(用于返回多个结果的SQL语句) |
| SQL类型 | Java数据类型 | getXxx()方法 | SQL类型 | Java数据类型 | getXxx()方法 |
|---|---|---|---|---|---|
| CHAR/VARCHAR | String | String getString() | LONGVARCHAR | String | InputStream getAsciiStream()/getUnicodeStream() |
| NUMERIC/DECIMAL | java.Math.BigDecimal | java.math.BigDecimal getBigDecimal() | BIT | Boolean | boolean getBoolean() |
| TINYINT | Integer | byte getByte() | SMALLINT | Integer | short getShort() |
| INTEGER | Integer | int getInt() | BIGINT | long | long getLong() |
| REAL | float | float getFloat() | FLOAT/DOUBLE | double | double getDouble() |
| BINARY/VARBINARY | byte[] | byte[] getBytes() | LONGVARBINARY | byte[] | InputStream getBinaryStream() |
| DATE | java.sql.Date | java.sql.Date getDate() | TIME | java.sql.Time | java.sql.Time getTime() |
| TIMESTAMP | java.sql.Timestamp | java.sql.Timestamp getTimestamp() | |||
rsltSet.close();//关闭查询结果集
stt.close();//关闭语句连接
conn.close();//关闭数据库连接
| 封装SQL语句的主要方法 | 说明 |
|---|---|
| void addBatch(String sql) | 向批执行表中添加SQL语句,在statement语句中增加用于数据库操作的SQL批处理语句 |
| void clearParameters() | 清除PreparedStatement中的设置参数 |
| boolean execute() | 执行SQL查询语句,可以是任何类型的SQL语句 |
| ResultSet executeQuery() | 执行SQL查询语句 |
| int executeUpdate() | 执行设置的预处理SQL语句,如insert, update, delete等,返回更新的列数 |
| ResultSet.MetaData getMetaData() | 进行数据库查询,获取数据库元数据 |
| 设置数据方法 | 设置数据方法 |
|---|---|
| void setArray(int index, Array x) | void setAsciiStream(int index, InputStream stream, int length) |
| void setBigDecimal(int index, BigDecimal x) | void setBinaryStream(int index, InputStream stream, int length) |
| void setCharacterStream(int index, InputStream stream, int length) | void setBoolean(int index, boolean x) |
| void setByte(int index, byte x) | void setBytes(int index, byte[] b) |
| void setDate(int index, Date x) | void setFloat(int index, float x) |
| void setInt(int index, int x) | void setLong(int index, long x) |
| void setRef(int index, int ref) | void setShort(int index, short x) |
| void setString(int index, String x) | void setTime(int index, Time x) |
PreparedStatement preStat=conn.prepareStatement("SELECT * FROM dbTableName");
preStat.setString(1,"b001");
ResultSet rst=preStat.executeQuery();
preStat.close();