//使用JDBC连接数据库:
conf.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/books
username=root
password=root
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;
}
}
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;
}
}
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);
}
}
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;
}
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;
}
import java.sql.ResultSet;
import java.sql.SQLException;
public interface ResultHandler {
public void resultSet(ResultSet rs)throws SQLException;
}