【问题标题】:Java program that uses SQL - compile into .jar使用 SQL 的 Java 程序 - 编译成 .jar
【发布时间】:2011-10-31 23:46:59
【问题描述】:

我的代码在 Eclipse 中编译和运行良好,但是当我尝试将其转换为 .jar 时,它给了我一堆错误。

代码、我用来编译成 .jar 的 .bat 文件和我的错误信息可以在这里找到:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Scanner;
import com.mysql.jdbc.PreparedStatement;


public class Main
{
        private static Scanner scn = new Scanner(System.in);

        public static void main(String[] argv) throws Exception
        {
                Class.forName("com.mysql.jdbc.Driver");

                Main main = new Main();
                int choice = 0;

                System.out.println("\t\tWelcome !\n\n");

                System.out.print(
                                           "1. Get Data From the table \"testtable\"\n"+
                                           "2. Insert data into the table \"testtable\"\n"+
                                           "Your choice?: "
                                                  );
                choice = scn.nextInt();
                scn.nextLine();

                switch( choice )
                {
                case 1:
                        main.getInfo();
                        break;

                case 2:
                        main.insertInfo();
                        break;

                default:
                        System.out.print("Was neither 1 or 2. Terminating program...");
                }


        }

        private void getInfo() throws Exception
        {
                //Class.forName("com.mysql.jdbc.Driver");

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "guest", "shahin33");
                PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT * FROM testtable");
                ResultSet result = statement.executeQuery();

                System.out.println();

                System.out.println("USERNAME\tPASSWORD");
                while( result.next() )
                {
                        System.out.println(result.getString(1) + "\t\t" + result.getString(2));
                }

                result.close();
                con.close();
        }

        private void insertInfo() throws Exception
        {
                System.out.print("\nUsername: ");
                String username = scn.nextLine();

                System.out.print("Password: ");
                String password = scn.nextLine().toLowerCase();

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "guest", "shahin33");
                PreparedStatement statement = (PreparedStatement) con.prepareStatement("INSERT INTO testtable VALUES(?,?)");
                statement.setString(1, username);
                statement.setString(2, password);
                statement.executeUpdate();

                System.out.println("\nUser information has been inserted to the database.\nRun the program again and chose 1 to see result.");
                statement.close();
                con.close();

        }
}

我用来编译成 .jar 的 .bat 文件:

@echo off

set /p fileName="Name for the file? Exclude .jar: "

copy *.java "Source Code"
javac *.java

jar -cvfm %fileName%.jar manifest.txt *.class "Source Code"

del *.class /f /q
del "Source Code" /f /q

我的错误信息:

C:\Users\shahin\Desktop\JavaComp>Compile_N_Whatnot.bat
Name for the file? Exclude .jar: comonXXXXXX
Main.java
        1 fil(er) kopierad(e).
Main.java:5: error: package com.mysql.jdbc does not exist
import com.mysql.jdbc.PreparedStatement;
                     ^
Main.java:51: error: cannot find symbol
                PreparedStatement statement = (PreparedStatement) con.prepa
tement("SELECT * FROM testtable");
                ^
  symbol:   class PreparedStatement
  location: class Main
Main.java:51: error: cannot find symbol
                PreparedStatement statement = (PreparedStatement) con.prepa
tement("SELECT * FROM testtable");
                                               ^
  symbol:   class PreparedStatement
  location: class Main
Main.java:75: error: cannot find symbol
                PreparedStatement statement = (PreparedStatement) con.prepa
tement("INSERT INTO testtable VALUES(?,?)");
                ^
  symbol:   class PreparedStatement
  location: class Main
Main.java:75: error: cannot find symbol
                PreparedStatement statement = (PreparedStatement) con.prepa
tement("INSERT INTO testtable VALUES(?,?)");

我的类路径:

C:\Program\Java\jdk1.7.\bin\;
C:\Users\shahin\Desktop\mysql-connector-java-5.1.18\com\mysql\jdbc\;
C:\Users\shahin\Desktop\mysql-connector-java-5.1.18\mysql-connector-java-5.1.18-bin.jar;

【问题讨论】:

    标签: java sql jar


    【解决方案1】:

    您需要设置类路径以包含所需的 jar 文件。至少,JDBC 驱动程序不在编译类路径中。

    我强烈建议不要在大多数简单的情况下使用批处理文件,至少要针对Ant 构建脚本。它非常了解 Java 的工作原理,并使这类问题更容易解决。

    【讨论】:

    • 我的类路径中有 jdbc 驱动程序
    • @user1021085 我对此表示怀疑,否则您将不会收到该错误。在批处理文件中明确设置它。为什么要复制源文件?
    • 我的类路径包括:C:\Users\shahin\Desktop\mysql-connector-java-5.1.18\com\mysql\jdbc\ 我在那里。我复制源文件是因为我想在jar里有源代码,所以朋友们可以查看源代码。
    • @user1021085 您需要明确命名 jar,如果您使用的是 JDK6 +,请使用通配符。
    • 谢谢。现在可以了。不过,我还有一个问题。我需要在连接数据库的每个方法中都执行“Class.forName”吗?
    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2021-02-09
    相关资源
    最近更新 更多