Linux版本 centOS 7,java连接mysql

在Linux下 使用java 访问数据库 ,

java 1.7版本,

mysql 8.0.33版本,

连接驱动   mysql-connector-java-5.1.49.jar

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class J {
    public static void main(String[] args) throws Exception{
        //1.导入jar包
        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
       // Class.forName("com.mysql.cj.jdbc.Driver");

        //3.获取连接
        Connection con = DriverManager.getConnection("jdbc:mysql://mysql.sqlpub.com:3306/huangjin","abc这里变了","fc12f7a5215e8e0a");

        //4.获取执行者对象
        Statement stat = con.createStatement();

        //5.执行sql语句,并且接收结果
        String sql = "SELECT * FROM user";
        ResultSet rs = stat.executeQuery(sql);

        //6.处理结果
        while(rs.next()) {
            System.err.println("\033[32m " + rs.getInt("uid") + "\t" + rs.getString("loginname")+ "\033[0m");
        }

        //7.释放资源
        con.close();
        stat.close();
        con.close();
    }
}

此时  java源码,与连接的jar包在一个目录下

[root@localhost src]# javac -cp mysql-connector-java-5.1.49.jar J.java
[root@localhost src]# java -cp ./mysql-connector-java-5.1.49.jar:. J
 

Fri Aug 11 19:46:57 CST 2023 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
 11111111       zhangsan

试验二

#查看linux系统

root@dcs-dd2b5627-0:/workspace/Java/src# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@dcs-dd2b5627-0:/workspace/Java/src# 

java 1.8版本,

mysql 8.0.33版本,

连接驱动   mysql-connector-j-8.0.33.jar

[root@localhost src]# javac -cp mysql-connector-j-8.0.33.jar J.java
[root@localhost src]# java -cp ./mysql-connector-j-8.0.33.jar:. J

你可能感兴趣的:(python,开发语言)