Error - Could not find or load main class

穿精又带淫゛_ 提交于 2019-12-04 07:03:31

问题


I want to connect my java program to connect with database and retrieve the data. its compile perfectly but runtime im getting this 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

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();
        }

    }
}

回答1:


You should download Driver from HERE

and JAR file need to add to project class path.

First Right click on you Eclipse Project, Project --> Build Path --> Configure Build Path. Under Libraries tab, click Add Jars or "Add External JARs" and add downloaded 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

SHOW VARIABLES WHERE Variable_name = 'port';



回答2:


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.

You haven't needed to call Class.forName() for about seven years, and you needs rendered to call newInstance() after that.




回答3:


Let's say your Java2Sql is inside test package.

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:




回答4:


Default port for mysql connections is 3306. So change this line

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

to this:

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


来源:https://stackoverflow.com/questions/25453794/error-could-not-find-or-load-main-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!