zoukankan      html  css  js  c++  java
  • java 链接数据库

    在pom文件中引入包:

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

     <version>8.0.11</version>

    <scope>runtime</scope>

    </dependency>

    链接数据库

    package com.qa.Base;
    
    import java.sql.*;
    
    public class CreatMysql {
        public static void main(String[] args) {
            //声明Connection对象
                    Connection con;
                     //驱动程序名
                     String driver = "com.mysql.cj.jdbc.Driver";
                     //URL指向要访问的数据库名mydata。tett1为数据库名称
                     String url = "jdbc:mysql://localhost:3306/test1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
                     //MySQL配置时的用户名
                     String user = "root";
                     //MySQL配置时的密码
                     String password = "12345678";
                     //遍历查询结果集
                     try {
                             //加载驱动程序
                             Class.forName(driver);
                             //1.getConnection()方法,连接MySQL数据库!!
                             con = DriverManager.getConnection(url,user,password);
                             if(!con.isClosed())
                                 System.out.println("Succeeded connecting to the Database!");
                             //2.创建statement类对象,用来执行SQL语句!!
                             Statement statement = con.createStatement();
                             //要执行的SQL语句
                             String sql = "select * from score";
                             //3.ResultSet类,用来存放获取的结果集!!
                         ResultSet rs = statement.executeQuery(sql);
                             System.out.println("-----------------");
                             System.out.println("执行结果如下所示:");
                             System.out.println("ID");
                             System.out.println("-----------------");
    
                             String id = null;
                             while(rs.next()){
                                     //获取id这列数据
                                     id = rs.getString("id");
                                     //输出结果
                                     System.out.println(id);
                                 }
                             rs.close();
                             con.close();
                         } catch(ClassNotFoundException e) {
                             //数据库驱动类异常处理
                             System.out.println("Sorry,can`t find the Driver!");
                           e.printStackTrace();
                             } catch(SQLException e) {
                             //数据库连接失败异常处理
                             e.printStackTrace();
                             }
    } }
    输出结果:
    
    
    
    注意事项:
    mysql版本,8以上
    //驱动程序名
    String driver = "com.mysql.cj.jdbc.Driver";
    //URL指向要访问的数据库名mydata
    String url = "jdbc:mysql://localhost:3306/test1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";如果驱动程序名没加“cj”则报错
    报错信息:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 
    The driver is automatically registered via the SPI and manual

    报错信息com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    数据库没有启动,启动数据库即可。




  • 相关阅读:
    SEH(Structured Exception Handling)详细解释
    Command Query Responsibility Segregation
    C#中Func和Expression的区别
    C#的yield return是怎么被调用到的?
    C#的static constructor抛了异常会怎么处理?
    developer应该知道的image知识(JPG和PNG)
    网站前台与后台的连接
    短消息类新旧服务代码对应表
    无线广告巨头渠道火拼
    中国移动下一代移动技术将选择LTE
  • 原文地址:https://www.cnblogs.com/lixianshengfitting/p/13883640.html
Copyright © 2011-2022 走看看