一、简单封装

package com.lgd.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {

public static Connection getConn()
{
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","liguodong");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

public static void close(Connection conn)
{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close(Statement stat,Connection conn)
{
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close(ResultSet rs, Statement stat,Connection conn)
{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
package com.lgd.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* 使用JDBCUtil工具类简化操作
* @author liguodong
*
*/

public class Demo12 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConn();

ps = conn.prepareStatement("insert into user(username) values(?)");
ps.setString(1, "扎西娅");
ps.executeUpdate();

} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCUtil.close(rs, ps, conn);
}
}

}

二、资源文件

​/src/db.properties​

mysqlDriver=com.mysql.jdbc.Driver
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleURL=jdbc\:oracle\:thin\:@localhost\:1521\:database
mysqlURL=jdbc\:mysql\://localhost\:3306/testjdbc
mysqlUser=root
mysqlPwd=liguodong
oracleUser=root
oraclePwd=liguodong

JDBC封装及资源文件_jdbc


JDBC封装及资源文件_jdbc_02


package com.lgd.jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;



public class JDBCUtil2 {

static Properties pros = null; //可以帮助和处理资源文件中的信息

static{//加载JDBCUtil2类的时候调用一次
pros = new Properties();
try {
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static Connection getMysqlConn()
{
try {
Class.forName(pros.getProperty("mysqlDriver"));
return DriverManager.getConnection(pros.getProperty("mysqlURL"),
pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

public static Connection getOracleConn()
{
try {
Class.forName(pros.getProperty("oracleDriver"));
return DriverManager.getConnection(pros.getProperty("oracleURL"),
pros.getProperty("oracleUser"),pros.getProperty("oraclePwd"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

public static void close(Connection conn)
{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close(Statement stat,Connection conn)
{
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close(ResultSet rs, Statement stat,Connection conn)
{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}