无法从Java连接到mysql

时间:2018-07-29 10:58:31

标签: java mysql jdbc

我正在制定一个程序,供面试官为特定候选人提交分数。采访时间是5.7.18,我必须在周二/周三之前提交程序。我无法连接到MySQL数据库。我认为用户创建中存在一些问题。从5台不同PC输入的标记应转到另一台PC上的MySQL数据库

这是我的url /密码/用户名-

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)

2 个答案:

答案 0 :(得分:1)

似乎mysql连接库未包含在项目中。按照建议的解决方案之一解决问题:

引用

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

答案 1 :(得分:0)

下载一个MySQL Connector / J并将.jar添加到您的类路径中。