1. 程式人生 > >jdbc封裝mySQL資料庫增刪改查

jdbc封裝mySQL資料庫增刪改查

package com.bdqn.dao;


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


/**
 * 工具類
 * 
 * @author Administrator
 * 
 */


public class BaseDao {
public static Connection conn = null;


public static Connection conn() {
try {
// 獲取反射載入驅動
Class.forName(Base.DRIVER);


conn = DriverManager.getConnection(Base.URL, Base.USERNAME,
Base.PWD);
System.out.println("連線成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("連線失敗");
}
return conn;
}


// 關閉流
public static void close(Connection conn, Statement st, ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}


// 增加資料
public static int insert(String sql, Object[] objs) {
int num = 0;
Connection conn2 = null;
PreparedStatement ps = null;
try {
// 獲取連線
conn2 = conn();
ps = conn2.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
if (objs != null && objs.length > 0) {
ps.setObject((i + 1), objs[i]);
}
}
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn2, ps, null);
}
return num;
}


// 查詢
public static ResultSet getResultSet(String sql, Object[] objs) {
Connection conn2 = conn();
ResultSet rs = null;
PreparedStatement ps = null;
try {
ps = conn2.prepareStatement(sql);
if (objs != null && objs.length>0) {
for (int i = 0; i < objs.length; i++) {
ps.setObject((i+1), objs[i]);
}
}
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}


}

package com.bdqn.dao;
/**
 * 基類屬性
 * @author Administrator
 *
 */
public class Base {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/student";
public static final String USERNAME="root";
public static final String PWD="root";
}

package com.bdqn.dao.impl;


import java.util.ArrayList;
import java.util.List;


import com.bdqn.entity.Dept;


/**
 * 介面
 * @author Administrator
 *
 */
public interface DeptImpl {
//修改資料
public int update(Dept dept);
//增加資料
public int insertSql(Dept dept);
//刪除資料
public int delete(int did);
//查詢
public List<Dept> select();

public List<Dept> select(int did);
}

package com.bdqn.entity;


/**
 * @author Administrator
 *
 */
public class Dept {
private int did;
private String dname;
private int dage;
private String dmesc;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getDage() {
return dage;
}
public void setDage(int dage) {
this.dage = dage;
}
public String getDmesc() {
return dmesc;
}
public void setDmesc(String dmesc) {
this.dmesc = dmesc;
}
public Dept(int did, String dname, int dage, String dmesc) {
this.did = did;
this.dname = dname;
this.dage = dage;
this.dmesc = dmesc;
}
public Dept() {

}
@Override
public String toString() {
return "Dpet [did=" + did + ", dname=" + dname + ", dage=" + dage
+ ", dmesc=" + dmesc + "]";
}
}

package com.bdqn.test;


import java.util.List;


import com.bdqn.entity.Dept;
import com.bdqn.util.DeptUtil;
/**
 * 測試類
 * @author Administrator
 *
 */
public class Test {
public static void main(String[] args) {
DeptUtil du=new DeptUtil();
/*Dept dept=new Dept(4, "Tom_C", 22, "hsgyrtf");
du.insertSql(dept);*/
Dept dept=new Dept();
List<Dept> select = du.select(2);
for (Dept dept2 : select) {
System.out.println(dept2.toString());
}

}
}

package com.bdqn.util;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.bdqn.dao.BaseDao;
import com.bdqn.dao.impl.DeptImpl;
import com.bdqn.entity.Dept;
/**
 * 實現類
 * @author Administrator
 *
 */
public class DeptUtil extends BaseDao implements DeptImpl {


@Override
public int update(Dept dept) {
String sql="update dept set dname=? where did=?";
Object[] objs={dept.getDname(),dept.getDid()};
int num = this.insert(sql, objs);
return num;
}


@Override
public int insertSql(Dept dept) {
String sql="insert into dept values(?,?,?,?)";
Object[] objs={dept.getDid(),dept.getDname(),dept.getDage(),dept.getDmesc()};
int num = this.insert(sql, objs);
return num;
}


@Override
public int delete(int did) {
String sql="delete from dept where did=?";
Object[] objs={did};
int num = this.insert(sql, objs);
return num;
}


@Override
public List<Dept> select(int did) {
String sql="select * from dept where did=?";
Object[] objs={did};
ResultSet rs = this.getResultSet(sql, objs);
List<Dept> list=null;
try {
list=new ArrayList<Dept>();
while(rs.next()){
Dept dept1=new Dept();
int did1 = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept1.setDid(did1);
dept1.setDname(dname);
dept1.setDage(dage);
dept1.setDmesc(dmesc);
list.add(dept1);
}


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.close(conn, null, rs);
}
return list;
}


@Override
public List<Dept> select() {
String sql="select * from dept";
ResultSet rs = this.getResultSet(sql, null);
List<Dept> list=null;
try {
list=new ArrayList<Dept>();
while(rs.next()){
Dept dept1=new Dept();
int did = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept1.setDid(did);
dept1.setDname(dname);
dept1.setDage(dage);
dept1.setDmesc(dmesc);
list.add(dept1);
}


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.close(conn, null, rs);
}
return list;
}


}