【JAVA】JDBC连接mySQL数据库常见错误(自身经历!!!)

目录

正确代码及操作:

 正确输出结果:

 错误原因:

(1)驱动类写错:

   (2)  url路径写错:


编译工具:SQLyog,IDEA,mysql-connector-java-8.0.17或mysql-connector-java-5.1.39-bin(需要自提)

链接:https://pan.baidu.com/s/1F9-A3x7Qj8XP4qQo5nWTmQ 
提取码:wlj3

链接:https://pan.baidu.com/s/1zq8znQbdZmerBGKlOTyslA 
提取码:zpr8

正确代码及操作:

SQLyog:

CREATE TABLE admin (
NAME VARCHAR(32) NOT NULL UNIQUE,
pwd VARCHAR(32) NOT NULL DEFAULT '') CHARACTER SET utf8;

INSERT INTO admin VALUES('tom' ,'123');

SELECT * FROM admin;

IDEA:

package com.hhb.jdbc.myjdbc;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

public class PreparedStatementDML_ {
    public static void main(String[] args) throws Exception {


        Scanner scanner = new Scanner(System.in);

        //让用户输入管理员名和密码
        System.out.print("请输入管理员的名字: ");  //next(): 当接收到 空格或者 '就是表示结束
        String admin_name = scanner.nextLine(); 
        System.out.print("请输入管理员的密码: ");
        String admin_pwd = scanner.nextLine();

        //通过Properties对象获取配置文件的信息

        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        //Class.forName(driver);//建议写上

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //3. 得到PreparedStatement
        //3.1 组织SqL , Sql 语句的 ? 就相当于占位符
        String sql = "select name , pwd  from admin where name =? and pwd = ?";
        //3.2 preparedStatement 对象实现了 PreparedStatement 接口的实现类的对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //3.3 给 ? 赋值
        preparedStatement.setString(1, admin_name);
        preparedStatement.setString(2, admin_pwd);

        //4. 执行 select 语句使用  executeQuery
        //   如果执行的是 dml(update, insert ,delete) executeUpdate()
        //   这里执行 executeQuery ,不要在写 sql

        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) { //如果查询到一条记录,则说明该管理存在
            System.out.println("恭喜, 登录成功");
        } else {
            System.out.println("对不起,登录失败");
        }

        //关闭连接
        resultSet.close();
        preparedStatement.close();
        connection.close();
        
    }

}

【JAVA】JDBC连接mySQL数据库常见错误(自身经历!!!)_第1张图片

配置文件mysql.properties中要写入user,password,url,driver使IDEA能与SQLyog相连。

【JAVA】JDBC连接mySQL数据库常见错误(自身经历!!!)_第2张图片

 正确输出结果:

【JAVA】JDBC连接mySQL数据库常见错误(自身经历!!!)_第3张图片

 错误原因:

(1)驱动类写错:

//当jar包版本为:mysql-connector-java-8.0.17.jar时

driver=com.mysql.cj.jdbc.driver

//当jar包版本为:mysql-connector-java-5.1.46.jar时

driver=com.mysql.jdbc.driver

(2)url路径写错:

url=jdbc:mysql://localhost:3306/test

//(1) jdbc:mysql:// 规定好表示协议,通过jdbc的方式连接mysql
//(2) localhost 主机,可以是ip地址
//(3) 3306 表示mysql监听的端口
//(4) test 连接到mysql dbms 的哪个数据库
//(5) mysql的连接本质就是前面学过的socket连接


若以上均正确仍然报错,原因可能为:

a.
报错:java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
原因:无法识别或代表多个时区如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值。

解决方法:在 url=jdbc:mysql://localhost:3306/test 后加?serverTimezone=UTC

变为:url = jdbc:mysql://localhost:3306/test?serverTimezone=UTC

b.

当输出结果为中文时,出现乱码时。 

解决方法:

在 url=jdbc:mysql://localhost:3306/test 后加 ?useUnicode=true&characterEncoding=UTF-8

变为:url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

若a和b同时发生

将url改为:

url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&&useUnicode=true&characterEncoding=UTF-8

你可能感兴趣的:(数据库,mysql,java)