JDBC找不到合适的驱动程序

编程入门 行业动态 更新时间:2024-10-27 08:27:53
本文介绍了JDBC找不到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试用Java创建自定义的MySQL类,但是我遇到了一些例外,这是我的代码:

I'm trying to make a custom MySQL class in Java but I'm getting a few exceptions, here's my code:

MySQL.java:

MySQL.java:

package evomon; import java.sql.*; public class MySQL { private static String hostname; private static String username; private static String password; private static String database; public MySQL(String host, String user, String pass, String db) { hostname = host; username = user; password = pass; database = db; } public static void Error(Exception ex) { System.out.println("Fatal Error: "+ex.getMessage()); } public static void SQLError(SQLException ex) { System.out.println("Fatal SQL Error: "+ex.getMessage()); } private static Connection connect() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch(Exception e){ Error(e); } String url = "jdbc:mysql://"+hostname+":3306/"+database; Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch(SQLException ex){ SQLError(ex); } return conn; } public static ResultSet result(String query) { Connection conn = connect(); PreparedStatement pst = null; ResultSet rs = null; try { pst = conn.prepareStatement(query); rs = pst.executeQuery(); } catch(SQLException ex){ SQLError(ex); } return rs; } public static void main(String args[]){}

}

Test.java:

Test.java:

package evomon; import java.sql.*; public class Test { public static void main(String args[]) { MySQL mysql = new MySQL("localhost", "root", "******", "souljaz"); ResultSet res = mysql.result("SELECT * FROM users"); }

}

运行Test时,我会在控制台中收到以下错误消息:

When I run Test I get these error messages in the console:

Fatal Error: com.mysql.jdbc.Driver Fatal SQL Error: No suitable driver found for jdbc:mysql://localhost:3306/souljaz Exception in thread "main" java.lang.NullPointerException at evomon.MySQL.result(MySQL.java:58) at evomon.Test.main(Test.java:8)

我对Java很陌生,请帮助.谢谢.

I'm quite new to Java so help appreciated. thanks.

推荐答案

下载最新版本的 Connector/J ,并将其包含在您的类路径中.如果使用的是IDE,则只需将jar包含在引用的库中即可.如果您要手动完成所有操作,请参阅此官方指南:

Download the lastest version of Connector/J and included it in your Classpath. If you are using an IDE this is only a matter of including the jar in your referenced libraries. If you are doing everything by hand, refer to this official guide:

java -cp "bin;lib/mysql-connector-java-5.1.30-bin" evomon.Test

更新:

在Eclipse中:在Package Explorer中右键单击您的项目,然后:

In Eclipse: Right click your project in Package Explorer, then:

Properties -> Java Build Path -> Add JARS... (if lib is in the project) or Add External JARS... (if it is external)

更多推荐

JDBC找不到合适的驱动程序

本文发布于:2023-10-18 23:42:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   驱动程序   合适   JDBC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!