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();
         }
     }
  • 相关阅读:
    Mongodb 插入数据的方式
    vue插槽(slot)的模板与JSX写法
    vue-resource+iview上传文件取消上传
    封装全局icon组件 svg (仿造element-ui源码)
    vue 修改框架less变量
    vue webpack多页面构建
    vue2 自定义全局组件(Loading加载效果)
    制作npm插件vue-toast-m实例练习
    iview+axios实现文件取消上传
    CSS Variables
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8545907.html
Copyright © 2011-2022 走看看