错误 - 无法找到或加载主类 [英] Error - Could not find or load main class

查看:226
本文介绍了错误 - 无法找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接我的java程序来连接数据库并检索数据。
编译完美但运行时
我得到这个错误:无法找到或加载主类

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

我已经安装了Java SQL驱动程序并将环境变量的jar路径添加为CLASSPATH

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

    }
}


推荐答案

您应该从这里下载驱动程序

和JAR文件需要添加到项目类路径。

and JAR file need to add to project class path.

首先右键单击Eclipse项目,项目 - - >构建路径 - >配置构建路径。在Libraries选项卡下,单击Add Jars或Add External JARs并添加下载的jar

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

不是100%肯定,但看起来你使用的是错误的端口号80.确保你的MySQL端口号是当前的以下语句

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';

这篇关于错误 - 无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆