初步封装
工具类JDBCUtils
package com.oplb.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/demo";
private static String username="root";
private static String password="123456";
private Connection conn=null;
private Statement st=null;
private ResultSet rs=null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123456");
}
public static void jdbcClose(Connection conn,Statement st,ResultSet rs){
try {
rs.close();
if(rs!=null){
rs=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
if(st!=null){
st=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
if(conn!=null){
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
测试类JDBCUtilstEST
package com.oplb.web;
import java.sql.Connection;
import com.oplb.web.JDBCUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtilsTest {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=JDBCUtils.getConnection();
st=conn.createStatement();
rs=st.executeQuery("select * from tb1");
while(rs.next()){
System.out.println(rs.getString(1)+":"+rs.getInt(2)+":"+rs.getFloat(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.jdbcClose(conn, st, rs);
}
}
}
二次封装
工具类:JDBCUtils
package com.oplb.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/demo";
private static String username="root";
private static String password="123456";
private Connection conn=null;
private Statement st=null;
private ResultSet rs=null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123456");
}
public void update(String sql){
try {
conn=getConnection();
st=conn.createStatement();
int bRet=st.executeUpdate(sql);
System.out.println(bRet);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcClose(conn,st);
}
}
public static void jdbcClose(Connection conn,Statement st){
try {
st.close();
if(st!=null){
st=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
if(conn!=null){
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void jdbcClose(Connection conn,Statement st,ResultSet rs){
try {
rs.close();
if(rs!=null){
rs=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
if(st!=null){
st=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
if(conn!=null){
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
测试类->
package com.oplb.web;
import java.sql.Connection;
import com.oplb.web.JDBCUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtilsTest {
public static void main(String[] args) {
String sql="insert tb1 values('wangwu',10,900.00)";
JDBCUtils jdbcutils=new JDBCUtils();
jdbcutils.update(sql);
}
}
查询结果
单元测试
package com.oplab.test;
import static org.junit.Assert.*;
import org.junit.Test;
import com.oplb.web.JDBCUtils;
public class JDBCUtilsTest {
@Test
public void test() {
JDBCUtils jdbcutils=new JDBCUtils();
String sql="insert tb1 values('wangwu',10,900.00)";
jdbcutils.update(sql);
}
}
单元测试结果
数据库查询结果