简体   繁体   English

找不到主班

[英]No main class found

Language : Java 语言 :Java

Program : Connecting to a database 程序 :连接到数据库

Question : I'm trying to connect the sqlite database by following TutorialsPoint tutorial but I keep getting the main class not found error. 问题 :我正在尝试通过遵循TutorialsPoint 教程来连接sqlite数据库,但我一直在遇到找不到主类的错误。

Implementation : My code is below followed by my terminal commands and folder structure screenshot. 实现 :下面是我的代码,然后是我的终端命令和文件夹结构屏幕截图。 But basically all my files are located in one folder including the sqlite jar file. 但基本上我的所有文件都位于一个文件夹中,其中包括sqlite jar文件。

import java.sql.*;

public class Test {
 public static void main(String[] args) {

 Connection c = null;

 try{
   Class.forName("com.sqlite.JDBC");
   c = DriverManager.getConnection("jdbc:sqlite:test.db");
 } catch(Exception e) {
   System.err.println(e.getClass().getName() + ": " + e.getMessage());
   System.exit(0);
 }

 System.out.println("Opened database successfully!");
 }
}

Terminal Commands 终端命令

javac Test.java
java -classpath ".;sqlite-jdbc-3.23.1.jar" Test

在此处输入图片说明

Your problem was that you're explicitly trying to load the class com.sqlite.JDBC , whereas the driver class name must've changed somewhere along the way. 您的问题是您正在明确尝试加载com.sqlite.JDBC类,而驱动程序类名称在此过程中必须已更改。

JDBC Type 4 drivers have added cleverness which allows you to specify only the connection URL, and the driver loads itself based on the beginning (ie jdbc:sqlite ). JDBC Type 4驱动程序增加了一些技巧 ,它允许您仅指定连接URL,并且该驱动程序根据开头(即jdbc:sqlite )进行加载。 No need to wonder what was the driver class's name. 无需怀疑驱动程序类的名称是什么。


Rant unrelated to the issue at hand: Rant与当前问题无关:

Unfortunately people read old tutorials written by less than experts, so we constantly see Class.forName() being used, as well as the more serious issue, which is using Statement instead of PreparedStatement . 不幸的是,人们阅读的不是专家编写的旧教程,因此我们经常看到正在使用Class.forName()以及更严重的问题,即使用Statement而不是PreparedStatement

My classpath option was incorrect. 我的classpath选项不正确。 I was on linux and was trying to do: 我在linux上并试图做:

java -classpath ".;sqlite-jdbc-3.23.1.jar" Test

the correct way was 正确的方法是

java -classpath ".:sqlite-jdbc-3.23.1.jar" Test

colon not semicolon. 冒号不是分号。 Unfortunately now it's giving me and error" ClassNotFoundException: com.sqlite.JDBC; 不幸的是现在它给了我错误” ClassNotFoundException:com.sqlite.JDBC;

I will look into this. 我将对此进行调查。 Thanks for the comments which helped me find the error 感谢您的评论,这有助于我发现错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM