环境:Windows10 + java8 + mysql 8.0.15 + mysql-connector-java-8.0.15.jar

mysql驱动程序目录

java 命令行JDBC连接Mysql

 

 项目目录

java 命令行JDBC连接Mysql

 

 代码:  

//package org.feilong.mysql;#这里因为太麻烦没有加package 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.mysql.jdbc.Driver;
public class Url2{
    public static final String URL = "jdbc:mysql://192.168.1.104:3306/gc";
    public static final String USER = "root";
    public static final String PASSWORD = "1";
    public static void main(String[] args) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");//加载驱动
        Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM books");
        
        try {
            while(rs.next()){
                System.out.println(rs.getString("author") + " ");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("not author");
        }
    }
}/* output:*///~

环境变量设置:

环境变量classpath直接设置到jar包

java 命令行JDBC连接Mysql

编译

java 命令行JDBC连接Mysql

运行

java 命令行JDBC连接Mysql

当然这里用了弃用的驱动,可以修改为 com.mysql.cj.jdbc.Driver

相关文章:

  • 2021-11-19
  • 2021-07-23
  • 2021-10-04
  • 2021-09-03
  • 2022-01-07
  • 2022-02-08
  • 2021-11-23
  • 2022-01-12
猜你喜欢
  • 2021-07-12
  • 2022-12-23
  • 2022-01-07
  • 2021-08-22
  • 2022-12-23
  • 2021-12-09
  • 2021-05-30
相关资源
相似解决方案