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

时间:2014-08-07 15:35:05

标签: java jdbc

我想用MYsql JDBC Connector编译.java文件

这是.jar文件的位置

d:\ MySQL的连接器的Java-5.1.31-bin.jar

这是我以前编译的......

javac -cp“D:\ mysql-connector-java-5.1.31-bin.jar”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();
}

当我从CMD ClassNotFoundException运行代码时抛出错误。

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

2 个答案:

答案 0 :(得分:5)

如果你使用命令提示符,你可以这样做 编译类

javac LocationServer.java

执行班级

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

记住它会;但不是:

答案 1 :(得分:1)

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

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

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