mysql数据库连接项目及基础增删改查封装代码

来源:互联网 发布:电子数据交换edi也称 编辑:程序博客网 时间:2024/06/05 10:08

连接数据库:

conf.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/books
username=root
password=root

ConnectionFactory.java:

package ui;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class ConnectionFactory {

private static String driver=null;
private static String url=null;
private static String username=null;
private static String password=null;

static{

Properties pro=new Properties();
InputStream ips=ConnectionFactory.class.getResourceAsStream("conf.properties");
try {
pro.load(ips);
driver=pro.getProperty("driver");
url=pro.getProperty("url");
username=pro.getProperty("username");
password=pro.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}

public static Connection getCon(){
Connection con=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}



}


JdbcMould.java:

package ui;

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

public class Mould {


public static int update(String sql,PreparedSetter preSet){

Connection con=ConnectionFactory.getCon();
PreparedStatement pstm=null;
int row=0;
try {
pstm=con.prepareStatement(sql);
if(preSet!=null){
preSet.setValue(pstm);
}
row=pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DButil.close(pstm, con);
}
return row;

}


public static void query(String sql,PreparedSetter preSet,ResultHandler resultHand){
Connection con=ConnectionFactory.getCon();
PreparedStatement pstm=null;
ResultSet rs=null;

try {
pstm=con.prepareStatement(sql);
if(preSet!=null){
preSet.setValue(pstm);
}
rs=pstm.executeQuery();
if(resultHand!=null){
resultHand.resultSet(rs);
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
DButil.close(rs, pstm, con);
}
}


public static int update(String sql,PreparedSetter preSet,Connection con){
int row=0;
PreparedStatement pstm=null;
try {
con.setAutoCommit(false);
pstm=con.prepareStatement(sql);
if(preSet!=null){
preSet.setValue(pstm);
}
row=pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DButil.close(pstm);
}
return row;

}

}


DButil.java

package ui;

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

public class DButil {

public static void close(ResultSet rs,PreparedStatement pstm,Connection con){

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

public static void close(PreparedStatement pstm,Connection con){
close(null,pstm,con);
}
public static void close(PreparedStatement pstm){
close(null,pstm,null);
}

}


PreparedSetter.java

package ui;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public interface PreparedSetter {

public void setValue(PreparedStatement pstm)throws SQLException;

}

ResultHandler.java

package ui;

import java.sql.ResultSet;
import java.sql.SQLException;

public interface ResultHandler {

public void resultSet(ResultSet rs)throws SQLException;

}