版权声明:. https://blog.csdn.net/WildestDeram/article/details/89647931
JDBC的工具类抽取
为了简化JDBC的开发,可以将一些重复的代码进行提取
编写工具类
JDBCutils.java
package com.jdbc.dream.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC工具类
* @author Jun
*
*/
public class JDBCutils {
/*静态常量*/
private static String url=null;
private static String username=null;
private static String password=null;
static {
// 加载属性文件并解析
Properties profile = new Properties();
// 通常情况下采用类的加载器的方式来进行获取
InputStream is = JDBCutils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
profile.load(is);
} catch (IOException e) {
e.printStackTrace();
}
// 赋值
url=profile.getProperty("url");
username=profile.getProperty("username");
password=profile.getProperty("password");
}
/**
* 连接数据库方法
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
/**
* 资源释放
*/
public static void release(Statement stat,Connection conn) {
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stat=null;
}
if(conn!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stat=null;
}
}
public static void release(ResultSet res,Connection conn,Statement stat) {
if(res!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
res=null;
}
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stat=null;
}
if(conn!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stat=null;
}
}
}
为了方便后期维护,可以将数据存在在配置文件中,注意:要存放啦src文件中
jdbc.properties
url=jdbc:mysql://localhost:3306/jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
username=root
password=1916
demo1.java
package com.dream.demo2;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.jdbc.dream.utils.JDBCutils;
public class demo1 {
@Test
public void jdbcdemo1() {
Connection conn = null;
Statement stat = null;
ResultSet res = null;
try {
// 连接数据库
conn = JDBCutils.getConnection();
// 编写SQL语句
String sql = "insert user values (null,'fff','666','大八')";
String sql1 = "select * from user";
// 执行SQL语句
stat=conn.createStatement();
int i = stat.executeUpdate(sql);
res = stat.executeQuery(sql1);
if(i>0) {
System.out.println("插入成功!");
while(res.next()) {
int id = res.getInt("uid");
String username = res.getString("username");
String pas = res.getString("password");
String name = res.getString("name");
System.out.println(id+"\t"+username+"\t"+pas+"\t"+name);
}
}
} catch (Exception e) {
// TODO: handle exception
} finally {
JDBCutils.release(res, conn, stat);
}
}
}