我试图从MySQL做一个简单的select语句,但由于某种原因我一直得到一个ClassNotFound异常。我在网上看到的所有例子都使用这个过程,所以我不知道出了什么问题。
注意:我不是要连接到本地主机。我正在尝试连接到您看到的IP。
try {
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://192.168.1.28/IT252";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "school", "sch00lp4ss");
String query = "SELECT * FROM representative";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
int id = rs.getInt("rep_id");
String firstName = rs.getString("rep_firstname");
String lastName = rs.getString("rep_lastname");
System.out.printf("%d, %s, %s %n", id, firstName, lastName);
}
st.close();
} catch (Exception e) {
e.printStackTrace();
}
堆栈跟踪。
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at it252.gui.addSale.<init>(addSale.java:48)
at it252.gui.mainWindow.lambda$new$2(mainWindow.java:37)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我不知道如何检查驱动程序是否已安装,我不知道如何在任何情况下将驱动程序添加到类路径中。 JDBC应该包括这个驱动程序吗?另外,我如何将驱动程序添加到类路径?我认为这应该都是内置的。
答案 0 :(得分:0)
大多数情况下ClassNotFoundException出现时,它找不到所需的驱动程序。确保正确安装了连接器并设置了CLASSPATH。尝试使用此Class.forName(“com.mysql.jdbc.Driver”)。newInstance();
答案 1 :(得分:0)
您可能还没有将mysqlconnector添加到您的库中。 你必须从https://dev.mysql.com/downloads/connector/j/下载它 然后将其放在项目的文件夹中。 从那里你可以将它添加到库中。例如在eclipse中你必须右键点击你的项目 - &gt;属性 - &GT; java build path-&gt;添加罐子 - &gt;从那里找到项目文件夹中的j连接器并添加它
我希望这可以提供帮助。顺便说一下,确保你的SQL查询没有错,你没有任何语法错误