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
数据库没有启动,启动数据库即可。

 





posted @ 2020-10-27 10:54  不屈的鸣人  阅读(151)  评论(0编辑  收藏  举报