我做了一個程序,它爲登錄表單創建了一個簡單的GUI。僅僅爲了測試的目的,我點擊「登錄」按鈕時,Java應該只是打印出MySQL數據庫的查詢結果。在MySQL中,我有一個名爲「test」的數據庫模式和一個名爲「login」的表。登錄表只有1行:「1,angelo,password」,列爲:loginID,Username和Password。Dr Java程序在向MySQL Workbench寫入查詢時會出現錯誤
MySQL Workbench用戶名是root。主機是localhost端口3306.我確信MySQL服務器當前正在運行(在任務管理器服務「MYSSQLSERVER」正在運行)
我在我的「mysql-connector-java-5.1.25-bin.jar」 DrJava的「Extra Classpath」文件夾(這是我用來編程的軟件)。
這裏是我的代碼:
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
}
下面是我收到的錯誤:
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)
的可能重複[掙扎 - 類未發現異常:在JAVA com.mysql.jdbc.Driver(http://stackoverflow.com/questions/6699796 /掙扎與類未找到異常com-mysql-jdbc-driver-in-java) –
該問題看起來有點類似,但我使用博士Java而不是NetBeans,所以我不知道這是否有所作爲 –