如何在debian10上安装jdbc以在java8中工作?

uujelgoq  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(354)

我正试图使用这段代码连接到Java8上的mysql数据库,但我得到一个异常,即找不到jdbc驱动程序类。debian 10的解决方案是什么?java.lang.classnotfoundexception:com.mysql.jdbc.driver

import java.sql.*; 

class MysqlCon {

 public static void main(String args[]) {  
    try {  
       Class.forName("com.mysql.jdbc.Driver");  
       Connection con = DriverManager.getConnection(
           "jdbc:mysql://localhost:3306/sonoo","root","root");  
       //here sonoo is database name, root is username and password  
       Statement stmt=con.createStatement();  
       ResultSet rs=stmt.executeQuery("select * from emp");  
       while(rs.next())  
       System.out.println(rs.getInt(1) + "  " + rs.getString(2) + " " + rs.getString(3));  
       con.close();  
    } catch(Exception e) {
         System.out.println(e);
    }  
 }  
}
flvlnr44

flvlnr441#

当从命令行调用java程序时,应该添加额外的参数-cp或-classpath,并引用jdbc库jar文件。
例子:


# Java directory

export JAVA_DIR="/usr/java8_64"

# Classpath

export CLASSPATH="/usr/lib/mysql-connector-java.jar"

# Start application

${JAVA_DIR}/bin/java -jar <MY_JAR_FILE_NAME> -classpath ${CLASSPATH}

相关问题