• JDBC-CRUD


    dbinfo.properties

    driverClass=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/day06
    username=root
    password=123456

    DBUtil.java

     package songyan.jdbc.utiltest;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ResourceBundle;
     public class DBUtil{
         private static String driverClass;
         private static String username;
         private static String url;
         private static String password;
         
         static {
             ResourceBundle rb= ResourceBundle.getBundle("dbinfo");
             driverClass=rb.getString("driverClass");
             username=rb.getString("username");
             url=rb.getString("url");
             password=rb.getString("password");
             
             try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
         }
         
         public static  Connection getConnection() throws SQLException
         {
             return DriverManager.getConnection(url,username,password);
         }
         
         public static  void closeAll(Connection conn,Statement sta,ResultSet rs)
         {
             if(rs!=null)
             {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs=null;
             }
             
             if(sta!=null)
             {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                sta=null;
             }
             
             if(conn!=null)
             {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn=null;
             }
         }
        
     
     }

    CRUDTest.java

     package songyan.jdbc.utiltest;
     
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    import songyan.jdbc.utiltest.DBUtil;
    import songyan.jdbc.entity.*;
    
     
     public class CRUDTest{
         
         public static void selectTest() throws Exception
         {
             Connection conn=null;
             Statement sta=null;
             ResultSet rs=null;
             
             conn=DBUtil.getConnection();
             
             sta=conn.createStatement();
             
             rs=sta.executeQuery("select * from users");
             
             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);
             }
             
             DBUtil.closeAll(conn, sta, rs);
             
         }
         
         public static void insertTest() throws SQLException
         {
             Connection conn=null;
             Statement sta=null;
             ResultSet rs=null;
             
             conn=DBUtil.getConnection();
             
             sta=conn.createStatement();
             
             System.out.println(sta.executeUpdate("insert into users values(7,'aaa','aaa','a@163.com','1981-12-04')"));        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void updateTest() throws SQLException
         {
             Connection conn=null;
             Statement sta=null;
             ResultSet rs=null;
             
             conn=DBUtil.getConnection();
             
             sta=conn.createStatement();
             
             System.out.println("影响了"+sta.executeUpdate("update users set name='bbb' where id='6'")+"行。");        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void deleteTest() throws SQLException
         {
             Connection conn=null;
             Statement sta=null;
             ResultSet rs=null;
             
             conn=DBUtil.getConnection();
             
             sta=conn.createStatement();
             
             System.out.println(sta.executeUpdate("delete from users where id='7'"));        
             
             DBUtil.closeAll(conn, sta, rs);
         }
         
         public static void main(String[] args) throws Exception
         {
             deleteTest();
         }
     }
  • 相关阅读:
    python for test
    python链接mysql pymysql
    MongoDB数据表添加字段
    NodeVisitor
    无法解决的错误
    一个点绕着另一个点旋转一定角度后的坐标
    2.0版本里程碑,研发日志
    osg Node getParentalNodePaths()报错
    TeslaManage 2.0编译日志
    机械臂模拟2.0
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8544449.html
Copyright © 2020-2023  润新知