• 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();
         }
     }
  • 相关阅读:
    Conversions
    Mispelling4
    A hard puzzle
    Easier Done Than Said?
    利用map可以对很大的数出现的次数进行记数
    A+B Coming
    结构体成员变量
    NSString 类介绍及用法
    复习回顾
    函数与方法对比
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8545907.html
Copyright © 2020-2023  润新知