gpt4 book ai didi

java - 我的 Java 程序没有打印我对 MySQL 进行的查询的结果

转载 作者:行者123 更新时间:2023-11-29 13:46:56 26 4
gpt4 key购买 nike

我编写了一个程序,为登录表单创建一个简单的 GUI。只是出于测试目的,我这样做是为了当您单击“登录”按钮时,Java 应该只打印 MySQL 数据库的查询结果。在 MySQL 中,我有一个名为“test”的数据库模式和一个名为“login”的表。登录表只有 1 行:“1,angelo,密码”,位于以下列:loginID、用户名和密码。

MYSQL 用户名是 root。主机是 localhost 端口 3306。MySQL 服务器当前正在运行。

我的“Extra Classpath”文件夹中有 DrJava 的“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)

根据我发布的内容,你能告诉我我做错了什么吗?谢谢。

最佳答案

您似乎在以下语句中使用了行数据来打印:

 System.out.println( rs.getString("angelo"));

您需要将列名称或列索引提供给rs.getString()

例如:

 System.out.println( rs.getString("user_name"));

 System.out.println( rs.getString(1));// assuming what you are lookin for is found in the first column

关于java - 我的 Java 程序没有打印我对 MySQL 进行的查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com