1.利用工具类Dbutils
1 package cn.neusoft.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class Dbutils { 10 private static final String driver="com.mysql.jdbc.Driver"; 11 private static final String url="jdbc:mysql://localhost:3306/shopping"; 12 private static final String name="root"; 13 private static final String pwd="root"; 14 15 private static Connection con=null; 16 private static PreparedStatement ps=null; 17 private static ResultSet rs=null; 18 //加载驱动 19 static{ 20 try { 21 Class.forName(driver); 22 } catch (ClassNotFoundException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 //获取连接 28 private static void getConnection(){ 29 try { 30 con=DriverManager.getConnection(url,name,pwd); 31 } catch (SQLException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 } 36 //关闭所有连接 37 private static void closeAll(){ 38 if(null!=rs){ 39 try { 40 rs.close(); 41 } catch (SQLException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 47 if(null!=ps){ 48 try { 49 ps.close(); 50 } catch (SQLException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 } 55 56 if(null!=con){ 57 try { 58 con.close(); 59 } catch (SQLException e) { 60 // TODO Auto-generated catch block 61 e.printStackTrace(); 62 } 63 } 64 } 65 66 //查询公用方法 67 public static ResultSet executeQuery(String sql,Object[] obj){ 68 getConnection(); 69 try { 70 ps=con.prepareStatement(sql); 71 if(null!=obj){ 72 for(int i=0;i<obj.length;i++){ 73 ps.setObject(i+1, obj[i]); 74 } 75 } 76 rs=ps.executeQuery(); 77 } catch (SQLException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 }finally{ 81 closeAll(); 82 } 83 return rs; 84 } 85 86 //增删改公用方法 87 public static int executeUpdate(String sql,Object[] obj){ 88 int count=0; 89 getConnection(); 90 try { 91 ps=con.prepareStatement(sql); 92 if(null!=obj){ 93 for(int i=0;i<obj.length;i++){ 94 ps.setObject(i+1, obj[i]); 95 } 96 } 97 count=ps.executeUpdate(); 98 } catch (SQLException e) { 99 // TODO Auto-generated catch block 100 e.printStackTrace(); 101 }finally{ 102 closeAll(); 103 } 104 return count; 105 } 106 }
2.编写测试model,dao和daoimpl
1 package cn.neusoft.test; 2 3 public class User { 4 5 public String uname; 6 7 public String pwd; 8 9 public String getUname() { 10 return uname; 11 } 12 13 public void setUname(String uname) { 14 this.uname = uname; 15 } 16 17 public String getPwd() { 18 return pwd; 19 } 20 21 public void setPwd(String pwd) { 22 this.pwd = pwd; 23 } 24 25 public User(String uname, String pwd) { 26 super(); 27 this.uname = uname; 28 this.pwd = pwd; 29 } 30 31 public User() { 32 super(); 33 } 34 35 }
1 package cn.neusoft.test; 2 3 public interface UserDao { 4 public int addUser(User user); 5 }
1 package cn.neusoft.test; 2 3 import cn.neusoft.util.Dbutils; 4 5 public class UserDaoImpl implements UserDao { 6 7 @Override 8 public int addUser(User user){ 9 String sql="insert into tbl_user (uname,pwd) values(?,?)"; 10 Object[] obj=new Object[]{user.getUname(),user.getPwd()}; 11 int a=0; 12 a=Dbutils.executeUpdate(sql, obj); 13 return a; 14 } 15 }
3.测试
1 package cn.neusoft.test; 2 3 import cn.neusoft.util.Dbutils; 4 5 public class TestUserDao { 6 static UserDao userdao=new UserDaoImpl(); 7 public static int testUser(User user){ 8 int a=userdao.addUser(user); 9 System.out.println(a); 10 return a; 11 } 12 public static void main(String[] args) { 13 User user=new User("8","8"); 14 testUser(user); 15 } 16 }
4.测试成功!
单独测试的话需要加入mysql-connector-java-5.0.8-bin.jar包,并右击Add to bulid path