首页 > 代码库 > Dao
Dao
package com.pra.www;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class EmpDao {
//数据库操作类
JDUtil jd = new JDUtil();
//改数据方法
public boolean changeEmp(int empno){
boolean b=false;
Connection con=jd.getConnection();
Statement st=null;
try {
st=con.createStatement();
int n = st.executeUpdate("update emp set ename=‘feng‘ where empno="+empno);
if(n>0)
b=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
//查询数据方法
public void getAllEmps(){
Connection con=jd.getConnection();
Statement st=null;
ResultSet rs=null;
try {
st=con.createStatement();
rs=st.executeQuery("select * from emp");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt(3)+"\t");
System.out.print(rs.getFloat(4)+"\t");
System.out.print(rs.getFloat(5)+"\t");
System.out.print(rs.getDate(6)+"\t");
System.out.print(rs.getString(7)+"\t");
System.out.println(rs.getInt(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//增加数据方法
public boolean saveEmp(int empno,String ename,float sal,Date d,String adr,int dn){
boolean b=false;
Connection con=jd.getConnection();
Statement st=null;
String sql="insert into emp(empno,ename,sal,birthday,address,deptno)"+
"values("+empno+",‘"+ename+"‘,"+sal+",‘"+d.toLocaleString()+"‘,‘"+adr+"‘,"+dn+")";
try {
st=con.createStatement();
int n = st.executeUpdate(sql);
if(n>0)
b=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
//删除数据方法
public void delEmp(String ename){
Connection con=null;
Statement st=null;
con=jd.getConnection();
try {
st=con.createStatement();
//创建完SQL语句,执行SQL语句
int n = st.executeUpdate("delete from emp where ename=‘"+ename+"‘");
if(n>0)
System.out.println("删除成功");
else
System.out.println("删除失败");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Dao