• JDBC与数据库的连接以及基本的操作


    package wf;
    
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Statement;
    
    public class driverManager {
        public static void main(String[] args) {
            Connection connection=null;
            Statement statement=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                 connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
    //       String sql="insert into customers values(111,'王锋','1553728655','1994-9-26')";
    //            String sql="delete from customers where id=123";
    //            String sql="update customers set name='小小'"+"where id=123 " ;
                 
                 String sql="select id,name,birth,email from customers";
                 
                 
                 statement=(Statement) connection.createStatement();
                ResultSet rs=statement.executeQuery(sql);
                while(rs.next()){
                    int id=rs.getInt(1);//下标从1开始
                    String name=rs.getString(2);
                    String email=rs.getString(3);
                    Date birth=rs.getDate(4);
                    System.out.println(id);
                    System.out.println(name);
                    System.out.println(email);
                    System.out.println(birth);
                    
                }
               statement.execute(sql);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(statement!=null){
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally{
                        if(connection!=null){
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    
                }
                }
        }
    }
  • 相关阅读:
    ZSSR
    分享mysql db 分区分表的shell
    oracle12c的CDB与PDB
    nodejs连接redis
    webservice 访问 网络共享文件夹 权限问题的解决方案
    闭包后感
    简单记录几个wpf学习上的问题[ObservableQueue]
    源码分析之Iterable&Collection(一)
    数据结构之树(三)
    数据结构之哈希表(二)
  • 原文地址:https://www.cnblogs.com/alhh/p/5535279.html
Copyright © 2020-2023  润新知