• 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();
                            }
                        }
                    }
                    
                }
                }
        }
    }
  • 相关阅读:
    MongoDB解析 2XML配置文件
    MongoDB解析 1理论基础
    JAVA GoF设计模式6大设计原则
    Kafka解析1快速入门
    JAVA GoF设计模式 1创建型设计模式
    B/S架构与C/S架构的区别
    【笔记】Oracle使用笔记
    如何通过WinDbg获取方法参数值
    Windows 启动过程
    ubuntu安装leveldb
  • 原文地址:https://www.cnblogs.com/alhh/p/5535279.html
Copyright © 2020-2023  润新知