Dr Java程序在向MySQL Workbench写入查询时,会出现错误

时间:2022-10-13 12:59:40

I made a program that makes a simple GUI for a login form. Just for testing purposes I made it so when you click the "Login" button, Java should just print out the query results from MySQL database. In MySQL I have a database schema called "test" and a table called "login". The login table only has 1 row: "1, angelo, password" under the columns: loginID, Username and Password.

我做了一个程序,为登录表单制作一个简单的GUI。出于测试目的,我将它设置为当您单击“Login”按钮时,Java只需打印来自MySQL数据库的查询结果。在MySQL中,我有一个名为“test”的数据库模式和一个名为“login”的表。登录表只有一行:“1,angelo, password”,列下:loginID,用户名和密码。

MySQL Workbench Username is root. Host is localhost port 3306. I made sure MySQL Server is currently running (In the Task Manager Services "MYSSQLSERVER" is running)

MySQL工作台用户名是根。主机是本地主机端口3306。我确保MySQL服务器当前正在运行(任务管理器服务“MYSSQLSERVER”正在运行)

I have the "mysql-connector-java-5.1.25-bin.jar" in my "Extra Classpath" folder for DrJava (that's the software I'm using to program).

我有“mysql-connector-java-5.1.25-bin。在我的“额外类路径”文件夹中的jar(这是我用来编程的软件)。

Here is my code:

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;




public class swing_sample extends JFrame
{
  //declaring our swing components
  JLabel l_name,l_pass, title;
  JTextField t_name;
  JPasswordField t_pass;     //A special JTextField but hides input text
  JButton button, button2;
  Container c;

  //a inner class to handling ActionEvents
  handler handle;

  //a separate class for processing database connection and authentication
  //database db;

  swing_sample()
  {
    super("Login form");

    c=getContentPane();
    //c.setLayout(new FlowLayout());
    c.setLayout(null);

    //extra classes
    //db=new database();
    handle =new handler();

    //swing components
    title = new JLabel("EAP Admin Log-In form");
    l_name=new JLabel("Username");
    l_pass=new JLabel("Password");
    t_name=new JTextField(10);
    t_pass=new JPasswordField(10);
    button=new JButton("Login");

    //adding actionlistener to the button
    button.addActionListener(handle);

    //add to contaienr
    c.add(title);
    c.add(l_name);
    c.add(t_name);
    c.add(l_pass);
    c.add(t_pass);
    c.add(button);
    //visual
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,530);


    //set positions, Size, 
    title.setLocation(175, 0); 
    title.setSize(200, 200);
    l_name.setLocation(50, 110); 
    l_name.setSize(100, 100);
    l_pass.setLocation(50, 210); 
    l_pass.setSize(100, 100);
    t_name.setLocation(200, 150); 
    t_name.setSize(200, 25);
    t_pass.setLocation(200, 250); 
    t_pass.setSize(200, 25);
    button.setLocation(100, 400); 
    button.setSize(100, 25);



  }
  public static void main(String args[])
  {
    swing_sample sample=new swing_sample();
  }

  //an inner class .You can also write as a separate class
  class handler implements ActionListener
  {
    //must implement method
    //This is triggered whenever the user clicks the login button
    public void actionPerformed(ActionEvent ae)
    {
      //checks if the button clicked
      if(ae.getSource()==button)
      {
        char[] temp_pwd=t_pass.getPassword();
        String pwd=null;
        pwd=String.copyValueOf(temp_pwd);
        System.out.println("Username,Pwd:"+t_name.getText()+","+pwd);



        Connection connection = null;

        try {
          // Load the JDBC driver
          String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver

          Class.forName(driverName);

          // Create a connection to the database
          String serverName = "localhost:3306";
          String mydatabase = "test";
          String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
          String username = "root";
          String password = "";
          connection = DriverManager.getConnection(url, username, password);


          System.out.println(url);

          Statement st = connection.createStatement();


ResultSet rs = st.executeQuery("select Username, Password from login where loginID   = 1");

          while(rs.next()) {
            System.out.println( rs.getString("Username"));
          }
          st.close();
          rs.close();
          connection.close(); 


        } catch (ClassNotFoundException e) {
          e.printStackTrace();        
          // Could not find the database driver
        } catch (SQLException e) {
          e.printStackTrace();     
          // Could not connect to the database
        }





     //The entered username and password are sent via "checkLogin()" which return `boolean`
        /*        if(db.checkLogin(t_name.getText(), pwd))
         {
         //a pop-up box
         JOptionPane.showMessageDialog(null, "You have logged in successfully","Success",
         JOptionPane.INFORMATION_MESSAGE);
         }
         else
         {
         //a pop-up box
         JOptionPane.showMessageDialog(null, "Login failed!","Failed!!",
         JOptionPane.ERROR_MESSAGE);
         }

         }//if
         */


      }//method

    }//inner class
  }//class
}

Here are the errors I am getting:

以下是我所犯的错误:

java.lang.ClassNotFoundException
at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at swing_sample$handler.actionPerformed(swing_sample.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个解决方案

#1


0  

It seems you have not placed the mysql connector jar file in your classpath. Try adding that jar and run your program. You can download the mysql connector official jdbc jar from here:

似乎您还没有将mysql connector jar文件放在类路径中。尝试添加jar并运行程序。您可以从这里下载mysql connector官方jdbc jar:

http://dev.mysql.com/downloads/connector/j/3.1.html

http://dev.mysql.com/downloads/connector/j/3.1.html

#1


0  

It seems you have not placed the mysql connector jar file in your classpath. Try adding that jar and run your program. You can download the mysql connector official jdbc jar from here:

似乎您还没有将mysql connector jar文件放在类路径中。尝试添加jar并运行程序。您可以从这里下载mysql connector官方jdbc jar:

http://dev.mysql.com/downloads/connector/j/3.1.html

http://dev.mysql.com/downloads/connector/j/3.1.html