java 如何在cmd中用java文件编译mysql JDBC驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25186557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:32:29  来源:igfitidea点击:

how to compile mysql JDBC driver with a java file in cmd

javajdbc

提问by Karedia Noorsil

I want to compile .java file with MYsql JDBC Connector

我想用 MYsql JDBC 连接器编译 .java 文件

This is where the .jar file is

这是 .jar 文件所在的位置

D:\mysql-connector-java-5.1.31-bin.jar

D:\mysql-connector-java-5.1.31-bin.jar

This is what I used to compile...

这是我用来编译的...

javac -cp "D:\mysql-connector-java-5.1.31-bin.jar" LocationServer.java

javac -cp "D:\mysql-connector-java-5.1.31-bin.jar" LocationServer.java

Code for LocationServer.java

LocationServer.java 的代码

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

public class LocationServer  {

private static final long serialVersionUID = 1L;
private Connection conn;
private final String driver = "com.mysql.jdbc.Driver";
private boolean connection;

protected LocationServer() {
    try {
        Class.forName(driver);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Driver Found");
    location = null;
    x = null;
    y = null;
    conn = null;
    connection = false;
}

public static void main(String[]args){
    LocationServer ls = new LocationServer();
}

When I run the the code from CMD ClassNotFoundException throws error.

当我运行 CMD ClassNotFoundException 中的代码时,会抛出错误。

How can I properly connect a .jar file with LocationServer.java so that MySql Driver class is found?

如何将 .jar 文件与 LocationServer.java 正确连接,以便找到 MySql Driver 类?

回答by SparkOn

Well if you are using command prompt you can do like this Compiling the class

好吧,如果您正在使用命令提示符,您可以像这样编译类

javac LocationServer.java

Executing the class

执行类

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

remember it will be ; but not :

记住它会是;但不是 :

回答by JFouad

in the last commande it's : not ;

在最后一个命令中它是:不是;

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

java -cp .:completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer

java -cp .:completePathOfMysqlConnector/mysql-connector-java-5.1.6.jar LocationServer