无法从java连接到mysql [重复]
Posted
技术标签:
【中文标题】无法从java连接到mysql [重复]【英文标题】:Unable to connect to mysql from java [duplicate] 【发布时间】:2018-07-29 10:58:30 【问题描述】:我正在为面试官制作一个程序,以便为特定候选人提交他们的分数。面试时间是 5.7.18,我必须在周二/周三之前提交程序。我无法连接到 mysql 数据库。我认为用户创建存在一些问题。从 5 台不同的 PC 输入的标记应转到不同 PC 中的 MySQL 数据库
这是我的网址/密码/用户名 -
private static final String URL = "jdbc:mysql://192.168.1.35:3306/interview";
private static final String USER = "root";
private static final String PASSWORD = "ubuntu";
这是我的 JAVA 代码 -
try
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
JOptionPane.showMessageDialog(null,"Connection Established!","Alert", JOptionPane.INFORMATION_MESSAGE);
String q1="select username from users";
Statement st = con.createStatement();
String usr=jTextField2.getText();
String psw=(new String(jPasswordField1.getPassword()));
ResultSet rs = st.executeQuery(q1);
boolean flag_usr=false;
while(rs.next())
if(usr.equals(rs.getString("username")))
flag_usr=true;
if(flag_usr==true)
String q2="select password from users";
ResultSet rs1 = st.executeQuery(q2);
boolean flag_psw=false;
while(rs1.next())
if(psw.equals(rs1.getString("password")))
this.dispose();
JOptionPane.showMessageDialog(null,"Login successful!","Alert", JOptionPane.INFORMATION_MESSAGE);
new Marks().setVisible(true);
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 interview.portal.Login.jButton1ActionPerformed(Login.java:178)
at interview.portal.Login.access$000(Login.java:21)
at interview.portal.Login$1.actionPerformed(Login.java:81)
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)
BUILD SUCCESSFUL (total time: 5 seconds)
【问题讨论】:
无论 5.7.18 是 2018 年 5 月 7 日还是 2018 年 7 月 5 日,我想说我们在这么晚的日期已经远远超出了“紧急” =(:0)。 此信息与您的问题无关,只会产生反对票:“我必须在周二/周三之前提交程序。”、“紧急”。 把你老师的名字和联系方式告诉我,我会给你一个延期。 【参考方案1】:项目中似乎没有包含 mysql 连接库。按照建议的解决方案之一解决问题:
参考
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse
【讨论】:
【参考方案2】:下载 MySQL Connector/J 并将 .jar 添加到您的类路径中。
【讨论】:
以上是关于无法从java连接到mysql [重复]的主要内容,如果未能解决你的问题,请参考以下文章
无法从 MYSQL Workbench 6.3 CE 连接到 MS SQL 服务器 [重复]
无法使用 JDBC 和 Spring 连接到 MySQL [重复]