主要是新建一个interface,然后实例化这个interface,然后对表内的数据封装。最后一个test,四个java如下
customer.java
package my_sql_test;
public class Customer {
//table == customer
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return id+":"+name+":"+email;
}
}
customerDao.java
package my_sql_test;
import java.util.List;
public interface CustomerDao {
public void add(Customer C);
public void update(Customer C);
public void delete(int id);
public Custome getCustomerById(int id);
public List<Customer> query();
//CRUD
}
customerDaoImpl.java
package my_sql_test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class CustomerDaoImpl implements CustomerDao {
@Override
public void add(Customer C) {
// TODO Auto-generated method stub
String sql="insert into customer (name,email)values (?,?)";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, C.getName());
pstmt.setString(2, C.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
@Override
public void update(Customer C) {
// TODO Auto-generated method stub
String sql="update customer set name=?,email=? where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, C.getName());
pstmt.setString(2, C.getEmail());
pstmt.setInt(3, C.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
String sql="delete from customer where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
@Override
public Custome getCustomerById(int id) {
// TODO Auto-generated method stub
String sql="select id,name,email from customer where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String name=rs.getString(2);
String email=rs.getString(3);
Customer C=new Custome();
C.setId(id);
C.setName(name);
C.setEmail(email);
return C;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
@Override
public List<Customer> query() {
// TODO Auto-generated method stub
String sql="select * from customer";
Connection conn=DBUtil.open();
try {
Statement stmt=conn.createStatement();
List<Customer> list=new ArrayList<Custome>();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String email=rs.getString(3);
Customer C=new Custome();
C.setId(id);
C.setName(name);
C.setEmail(email);
list.add(C);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
}
testDao.java
package my_sql_test;
import java.util.List;
public class testDao {
public static void main(String[] args){
CustomerDao dao=new CustomerDaoImpl();
Customer C=new Customer();
Customer c=dao.getCustomerById(2);
System.out.println(c);
/* List<Customer> list=dao.query();
System.out.println(list);
*/
// dao.delete(1);
/*C.setId(1);
C.setName("i");
C.setEmail("ii");
dao.update(C);*/
/*C.setName("tree");
C.setEmail("itree");
dao.add(C);*/
}
}