简体   繁体   English

错误 - 无法找到或加载主类

[英]Error - Could not find or load main class

I want to connect my java program to connect with database and retrieve the data.我想连接我的java程序以连接数据库并检索数据。 its compile perfectly but runtime im getting this Error : Could not find or load main class它的编译完美,但运行时我收到此Error : Could not find or load main class

i have installed the Java SQL driver and added the jar path to the Environmental variable as CLASSPATH我已经安装了 Java SQL 驱动程序并将 jar 路径添加到环境变量中作为 CLASSPATH

import java.sql.*;
public class Java2Sql{
    public static void main(String args[]){
        String url = "jdbc:mysql://localhost:80/";
        String dbName = "test";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "root";

        try{
            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url+dbName,userName,password);

            Statement stmt = conn.createStatement();
            String strsql = "SELECT * FROM student";

            ResultSet res = stmt.executeQuery(strsql);

            while(res.next()){
                System.out.println("ID :"+res.getString(1));
                System.out.println("Name :"+res.getString(2));
                System.out.println("Tel :"+res.getString(3));
                System.out.println("City :"+res.getString(4));
            }
            res.close();
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}

Let's say your Java2Sql is inside test package.假设您的Java2Sqltest包中。

Folder Structure:文件夹结构:

在此处输入图片说明

And the code you are trying to run is as below.您尝试运行的代码如下。

Code:代码:

package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Java2Sql {
    public static void main(String args[]) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test", "username", "pwd"); // Test DB
            System.out.println("Connected.");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

Commands you need to compile and run would be:您需要编译和运行的命令是:

javac Java2Sql.java
java -classpath .;test/mysql-connector-java-5.0.4-bin.jar test.Java2Sql

Compilation and Execution:编译和执行:

在此处输入图片说明

The class named in the manifest Main-class entry doesn't exist in your JAR file, or possibly there is no Main-class: entry in the manifest.清单 Main-class 条目中命名的类在您的 JAR 文件中不存在,或者清单中可能没有 Main-class: 条目。

You haven't needed to call Class.forName() for about seven years, and you needs rendered to call newInstance() after that.大约七年来,您不需要调用 Class.forName(),之后需要渲染以调用 newInstance()。

Default port for mysql connections is 3306. So change this line mysql 连接的默认端口是 3306。所以改变这一行

String url = "jdbc:mysql://localhost:80/";

to this:对此:

String url = "jdbc:mysql://localhost:3306/";

You should download Driver from HERE你应该从这里下载驱动程序

and JAR file need to add to project class path.和 JAR 文件需要添加到项目类路径中。

First Right click on you Eclipse Project, Project --> Build Path --> Configure Build Path.首先右键单击您的 Eclipse 项目,项目 --> 构建路径 --> 配置构建路径。 Under Libraries tab, click Add Jars or "Add External JARs" and add downloaded jar在 Libraries 选项卡下,单击 Add Jars 或“Add External JARs”并添加下载的 jar

Not 100% sure but looks like you are using wrong port number 80. Make sure your MySQL port number is current by below statement不是 100% 确定,但看起来您使用了错误的端口号 80。通过以下语句确保您的 MySQL 端口号是最新的

SHOW VARIABLES WHERE Variable_name = 'port';

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

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