ログインフォームの簡単なGUIを作るプログラムを作りました。テスト目的で、「ログイン」ボタンをクリックすると、Java が MySQL データベースからクエリ結果を出力するようにしました。MySQL には、「test」というデータベース スキーマと「login」というテーブルがあります。ログイン テーブルには、loginID、Username、Password の列の下に「1, angelo, password」という 1 つの行しかありません。
MYSQL ユーザー名は root です。ホストは localhost ポート 3306 です。MySQL サーバーは現在実行中です。
DrJavaの「Extra Classpath」フォルダーに「mysql-connector-java-5.1.25-bin」があります(これは、プログラムに使用しているソフトウェアです)。
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.Statement;
import java.sql.SQLException;
( GUI のコードはここに入る )
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);
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from login");
while(rs.next()) {
System.out.println( rs.getString("username"));
}
st.close();
rs.close();
connection.close();
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
これを実行するたびにエラーは発生しません。何も出力されないだけです。
編集: e.printStackTrace を追加すると、次のエラーが発生します。
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:108)
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)
私が投稿した内容に基づいて、私が間違っていることを教えてもらえますか? ありがとう。