zoukankan      html  css  js  c++  java
  • CRUD_PreparedStatement

     package songyan.jdbc.crud;
     
    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;
    
    import songyan.jdbc.util.DBUtil;
    import songyan.jdbc.entity.*;
    
     
     public class CRUD_prepared{
         
         public static void selectTest() throws Exception
         {
             Connection conn=null;
             PreparedStatement sta=null;
             ResultSet rs=null;
             String sql="select * from users where name=? and password=?";
             
             conn=DBUtil.getConnection();
             
             sta=conn.prepareStatement(sql);
             sta.setString(1, "zhansan");
             sta.setString(2, "123");
             
             rs=sta.executeQuery();
             
             List<User> l= new ArrayList<User>();         
             while(rs.next())
             {
                User u= new User();
                u.setId(rs.getInt("id"));
                u.setName(rs.getString("name"));
                u.setPassword(rs.getString("password"));
                u.setEmail(rs.getString("email"));
                u.setBirthday(rs.getDate("birthday"));
                l.add(u);
             }
             
             for(User u:l)
             {
                 System.out.println(u.getId()+" "+u.getName());
             }
             
             DBUtil.closeAll(conn, sta, rs);
             
         }
         
         public static void insertTest() throws SQLException
         {
             Connection conn=null;
             PreparedStatement sta=null;
             ResultSet rs=null;
             String sql="insert into users values(1,'a0','b0','a@163.com','1981-12-04')";
             
             conn=DBUtil.getConnection();
             
             sta=conn.prepareStatement(sql);
    
             System.out.println(sta.executeUpdate());        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void updateTest() throws SQLException
         {
             Connection conn=null;
             PreparedStatement sta=null;
             ResultSet rs=null;
             String sql="update users set name='lisi' where id='4'";
             conn=DBUtil.getConnection();
             
             sta=conn.prepareStatement(sql);
             
             System.out.println("影响了"+sta.executeUpdate());        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void deleteTest() throws SQLException
         {
             Connection conn=null;
             PreparedStatement sta=null;
             ResultSet rs=null;
             String sql="delete from users where name='bbb'";
             
             conn=DBUtil.getConnection();
             
             sta=conn.prepareStatement(sql);
             System.out.println(sta.executeUpdate());        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void main(String[] args) throws Exception
         {
             deleteTest();
         }
     }
  • 相关阅读:
    ASP.Net设计时需要考虑的性能优化问题 转载自http://blog.sina.com.cn/s/blog_3d7bed650100055p.html
    Jqeruy dropdownlist 联动
    Sound Recording in Windows Phone 7
    Windows Phone Performance 系列网址集合
    【xml]: Read XML with Namespace resolution using XLinq.XElement
    编程基础字符篇(2)
    每日总结一:
    每日总结5:
    Control usage: (1) Windows Phone 7: Popup control
    编程基础字符篇(3)
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8545907.html
Copyright © 2011-2022 走看看