JDBC中手写工具类,对JDBC进行封装
package cn.com.jdbc;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
@SuppressWarnings("all")
public class JDBCUtils {
static Properties pros = null;//可以帮助和读取资源文件中的信息
static { //加载这个类时候调用
pros = new Properties();
try {
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
//获得mysql的连接
public static Connection getMySqlCoon() {
try {
Class.forName(pros.getProperty("mysqldriver"));
return DriverManager.getConnection
(pros.getProperty("mysqlURL"),pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void close(ResultSet rs, Statement ps, Connection coon) {
try {
if (rs != null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if (coon != null) {
coon.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement ps, Connection coon) {
try {
if (ps != null) {
ps.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if (coon != null) {
coon.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection coon) {
try {
if (coon != null) {
coon.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement ps) {
try {
if (ps != null) {
ps.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs, Statement ps) {
try {
if (rs != null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
这是资源文件
mysqldriver=com.mysql.jdbc.Driver
mysqlURL=jdbc:mysql://localhost:3306/testjdbc
mysqlUser=root
mysqlPwd=123456
测试类
package cn.com.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@SuppressWarnings("all")
public class TestJDBCUTiles {
public static void main(String[] args) {
Connection coon = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
coon = JDBCUtils.getMySqlCoon();
ps = coon.prepareStatement("insert into t_user2(username,pwd) values(?,?)");
ps.setObject(1,"晓晓");
ps.setObject(2,"123456");
ps.execute();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,ps,coon);
}
}
}