java 命令行JDBC连接Mysql
环境:Windows10 + java8 + mysql 8.0.15 + mysql-connector-java-8.0.15.jar
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包
编译
运行
当然这里用了弃用的驱动,可以修改为 com.mysql.cj.jdbc.Driver