zoukankan      html  css  js  c++  java
  • Java连接MySQL8.0以上版本

     //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.用户信息和URL
            String url="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
            String username="root";
            String password="123456";
            //3.连接成功,数据库对象,Connection 代表数据库
            Connection connection = DriverManager.getConnection(url, username, password);
            //4.执行SQL的对象 statement 执行sql的对象
            Statement statement = connection.createStatement();
            //5.执行SQL的对象 去 执行SQL
            String sql="SELECT * FROM student";
            ResultSet resultSet = statement.executeQuery(sql);//返回的结果集
            while(resultSet.next()){
                System.out.println("id="+resultSet.getObject("id"));
                System.out.println("姓名="+resultSet.getObject("name"));
                System.out.println("年龄="+resultSet.getObject("age"));
                System.out.println("学号="+resultSet.getObject("stunum"));
                System.out.println("***********************************************");
            }
            //6.释放连接
            resultSet.close();
            statement.close();
            connection.close();
  • 相关阅读:
    mysql允许远程访问
    ubuntu pip install MySQL-python mysql_config not found
    ubuntu 阿里云源
    V
    KMP算法之next函数解释(大量的反证法 和数学归纳法来袭)
    日常ACM题目
    F
    J
    中缀表达式求值 ,中缀表达转化为后缀表达式求值,
    数据结构
  • 原文地址:https://www.cnblogs.com/IanIan/p/13915794.html
Copyright © 2011-2022 走看看