2017年12月21日 22:52:27
封装 做配置文件
utils包 创建一个Contants类 负责驱动和连接的utl 用户名 密码
package com.jdbc.utils;
public class Contants {
public static final String driver="com.mysql.jdbc.Driver";
public static final String url ="jdbc:mysql://localhost:3306/dept";
public static final String user ="root";
public static final String pwd="root";
}
dao包BaseDaod类 加载驱动 获得连接 关闭资源 增删改 查 方法
package com.jdbc.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;
import com.jdbc.utils.Contants;
import com.sun.org.apache.regexp.internal.recompile;
public class BaseDao {
private static Connection conn=null;
//加载驱动
public static Connection getConnection(){
try {
//加载驱动
Class.forName(Contants.driver);
//获取链接
conn=DriverManager.getConnection(Contants.url, Contants.user, Contants.pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增删改
public static int update(String sql,Object[] objects){
int num=0;
PreparedStatement ps=null;
try {
conn=getConnection();
ps=conn.prepareStatement(sql);
if (objects!=null&&objects.length>0) {
for (int i = 0; i < objects.length; i++) {
ps.setObject((i+1), objects[i]);
}
}
num=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(conn, ps, null);
}
return num;
}
//查询
public static ResultSet getrResultSet(String sql,Object[] objects){
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=getConnection();
ps=conn.prepareStatement(sql);
if (objects!=null&&objects.length>0) {
for (int i = 0; i < objects.length; i++) {
ps.setObject((i+1),objects[i]);
}
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
entity包 创建一个Dept类实现序列化能力接口
package com.jdbc.entity;
import java.io.Serializable;
public class Dept implements Serializable{
private int did;
private String dname;
private int dage;
private String dmesc;
public Dept() {
super();
}
public Dept(int did, String dname, int dage, String dmesc) {
super();
this.did = did;
this.dname = dname;
this.dage = dage;
this.dmesc = dmesc;
}
@Override
public String toString() {
return "Dept [did=" + did + ", dname=" + dname + ", dage=" + dage
+ ", dmesc=" + 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;
}
}
dao包 创建一个DeptDao接口类
package com.jdbc.dao;
import java.util.List;
import com.jdbc.entity.Dept;
public interface DeptDao {
int insert(Dept dept);
int updateDept(Dept dept);
int deleteDept(int did);
//全查询
List<Dept> selectAllDept();
//根据id查询
List<Dept> selectDept(int did);
}
dao包下的impl包下创建一个DeptImpl类实现接口的方法
package com.jdbc.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jdbc.dao.BaseDao;
import com.jdbc.dao.DeptDao;
import com.jdbc.entity.Dept;
public class DeptImpl extends BaseDao implements DeptDao{
@Override
public int insert(Dept dept) {
// TODO Auto-generated method stub
String sql="insert into dept values(?,?,?,?)";
Object[] objects={dept.getDid(),dept.getDname(),dept.getDage(),dept.getDmesc()};
int num=this.update(sql, objects);
return num;
}
@Override
public int updateDept(Dept dept) {
// TODO Auto-generated method stub
String sql="update dept set dname=? where did=?";
Object[] objects={dept.getDname(),dept.getDid()};
int num=this.update(sql, objects);
return num;
}
@Override
public int deleteDept(int did) {
// TODO Auto-generated method stub
String sql="delete from dept where did=?";
Object[] objects={did};
int num=this.update(sql, objects);
return 0;
}
@Override
public List<Dept> selectAllDept() {
// TODO Auto-generated method stub
String sql="select * from dept";
List<Dept> list=null;
try {
ResultSet rs=this.getrResultSet(sql, null);
list=new ArrayList<Dept>();
while (rs.next()) {
Dept dept=new Dept();
int did=rs.getInt("did");
String dname=rs.getString("dname");
int dage=rs.getInt("dage");
String dmesc=rs.getString("dmesc");
dept.setDid(did);
dept.setDname(dname);
dept.setDage(dage);
dept.setDmesc(dmesc);
list.add(dept);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List<Dept> selectDept(int did) {
// TODO Auto-generated method stub
String sql="select * from dept where did=?";
Object[] objects={did};
List<Dept> list=null;
try {
ResultSet rs=this.getrResultSet(sql, objects);
list=new ArrayList<Dept>();
while (rs.next()) {
Dept dept=new Dept();
//int did=rs.getInt("did");
String dname=rs.getString("dname");
int dage=rs.getInt("dage");
String dmesc=rs.getString("dmesc");
//dept.setDid(did);
dept.setDname(dname);
dept.setDage(dage);
dept.setDmesc(dmesc);
list.add(dept);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
创建一个测试类
测试增删改查的封装使用成功与否
package com.jdbc.test;
import java.util.ArrayList;
import java.util.List;
import com.jdbc.dao.DeptDao;
import com.jdbc.dao.impl.DeptImpl;
import com.jdbc.entity.Dept;
public class Test {
public static void main(String[] args) {
DeptDao dao=new DeptImpl();
//插入
/*Dept dept=new Dept(10, "hao", 10, "dajiahao");
int num=dao.insert(dept);
System.out.println(num);*/
//修改
/*Dept dept=new Dept();
dept.setDname("aaa");
dept.setDid(7);
int num=dao.updateDept(dept);
System.out.println(num);*/
//删除
/*int num=dao.deleteDept(5);
System.out.println(num);*/
//全查询
/*List<Dept> list=dao.selectAllDept();
for (Dept dept : list) {
System.out.println(dept.toString());
}*/
//单条查询
/*List<Dept> list=dao.selectDept(4);
for (Dept dept : list) {
System.out.println(dept.toString());
}*/
}
}
做配置文件
新建一个.txt文件 写好驱动和url 用户名 密码 后把名字和后缀名改成 database.properties
把它复制到src里
改一下BaseDao类 加载驱动的那个方法
public class BaseDao{
static{//静态代码块 在项目启动的时候就加载 优先于其他方法执行
init();
}
//记得try catch
//初始化配置文件
public void init(){
//得到文件的路径
String fileName="database.properties";
//文件加载到流里面
inputstream is=BaseDao.class.getClassLoader().getResourceAsStream(fileName);
properties.load(is);//读取文件信息
}
}
还得该以Contants类 去掉finilly不变
Contants.driver=properties.getProperty("driver");
Contants.url=properties.getProperty("url");
Contants.user=properties.getProperty("user");
Contants.pwd=properties.getProperty("password");
。