操作步骤:
1.注册数据库驱动程序
2.获取数据库连接对象
3.建立数据库操作语句
4.执行并返回结果
5.释放连接
CODE
1. 配置peoperties属性文件
DRIVERS=oracle.jdbc.driver.OracleDriver
URL=jdbc:oracle:thin:@localhost:1521:orcl
USERNAME=user
PASSWORD=password
2. 解析配置文件属性
package com.jdbc; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class DBConfig extends Properties{ private static final long serialVersionUID = 6988299229093520679L; private static DBConfig dbConfig = null; private DBConfig() { try{ InputStream inStream = DBConfig.class.getResourceAsStream("/dbConfig.properties"); this.load(inStream); }catch (IOException e) { e.printStackTrace(); } } public static DBConfig getInstance() { if(dbConfig == null){ synchronized (DBConfig.class) { if(dbConfig == null){ dbConfig = new DBConfig(); } } } return dbConfig; } } |
3. 加载驱动获取连接操作
package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtils { private static String DB_DRIVERS; private static String DB_URL; private static String DB_USER; private static String DB_PASSWORD; static { DBConfig config = DBConfig.getInstance(); DB_DRIVERS = config.getProperty(DBConstant.DB_DRIVERS); DB_URL = config.getProperty(DBConstant.DB_URL); DB_USER = config.getProperty(DBConstant.DB_USERNAME); DB_PASSWORD = config.getProperty(DBConstant.DB_PASSWORD); } /** * 获取数据库连接 * @return conn 数据库连接 * @throws ClassNotFoundException if the class cannot be located * @throws SQLException if a database access error occurs */ public static Connection getConnection() throws ClassNotFoundException, SQLException{ Connection conn = null; try { // 1.注册数据库驱动程序 Class.forName(DB_DRIVERS); // 2.获取数据库连接对象 conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); } catch (ClassNotFoundException e) { throw new ClassNotFoundException("数据库驱动注册失败" + e.getMessage()); }catch (SQLException e) { throw new SQLException("获取数据库连接对象失败" + e.getMessage()); } return conn; } /** * 释放数据库连接 */ public static void release(ResultSet rs, Statement stmt, Connection conn) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } |
4.操作
package com.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestDb { public static void main(String[] args) throws Exception { String sql = "select * from producttype where typeid = ?"; // 获取数据库连接对象 Connection conn = DBUtils.getConnection(); // 建立数据库操作语句 PreparedStatement pstm = conn.prepareStatement(sql); pstm.setInt(1, 21); // 执行操作语句 ResultSet rs = pstm.executeQuery(); // 获取执行结果 while(rs.next()){ System.out.println(rs.getInt("typeid")); } // 关闭连接 DBUtils.release(rs, pstm, conn); } } |
常用JDBC驱动
Mysql | http://www.mysql.com/products/connector/j/ |
Oracle | http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html |
SQL Server by jTDS | http://sourceforge.net/project/showfiles.php?group_id=33291 |
Postgres | http://jdbc.postgresql.org/download.html |
SAP DB | http://www.sapdb.org/sap_db_jdbc.htm |
SyBase by jTDS | http://jtds.sourceforge.net/ |