gpt4 book ai didi

mysql - 如何仅使用 Jdbc 从 MySQL 验证后检索特定用户的数据?

转载 作者:行者123 更新时间:2023-11-29 18:45:29 25 4
gpt4 key购买 nike

这是一个代码片段......我只是想让它在用户成功登录验证后检索该用户的信息......(我只使用jdbc和 Mysql 服务器,我从控制台获取输入......并假设数据库表有您选择的 4 个条目。)

表名称: 结果

我的表属性是: 名称|卷|数学|物理|化学

package com.connect.register;

import java.sql.*;
import java.util.*;

import static com.connect.register.Dbconnection.*;

public class ShowResult {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");

System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(URL, USER, PAS);
System.out.println("Connected database successfully...");

String SQL = "Select * from studentconnect.result Where Roll=?";

System.out.println("Creating statement...");
stmt = conn.prepareStatement(SQL);

Scanner scn = new Scanner(System.in);
System.out.print("Enter roll no. to see result:");
int roll = scn.nextInt();
scn.close();
stmt.setInt(1, roll);

rs = stmt.executeQuery();

if (rs.next()) {
System.out.println("Login Successfull");
return;
} else {
System.out.println("Please enter Correct roll no.");
}
**while (rs==roll) {
// Retrieve by column name
String Name = rs.getString("Name");
int Roll = rs.getInt("Roll");
int Math = rs.getInt("Math");
int Physics = rs.getInt("Physics");
int Chemistry = rs.getInt("Chemistry");
// Display values
System.out.println("Name: " + Name);
System.out.println("Roll: " + Roll);
System.out.println("Math " + Math);
System.out.println("Physics: " + Physics);
System.out.println("Chemistry: " + Chemistry);
}
rs.close();**

}

}

最佳答案

如前所述,rs 与 roll 完全不同。 rs 代表包含所有已发送到数据库的查询响应的 ResulSet。由于您已在声明中输入了卷号,并且我假设卷号是唯一的,因此您的代码可能如下所示:

rs = stmt.executeQuery();
if ( rs.next()) {
System.out.println("Login successfull");
System.out.println("Name: " + rs.getString("Name") + "\n"
+ "Roll: " + rs.getInt("Roll") + "\n"
+ "Math: " + rs.getInt("Math") + "\n"
+ "Physics: + rs.getInt("Physics") + "\n"
+ "Chemistry: " + rs.getInt("Chemistry"));
rs.close();
} else {
System.out.println("Please enter Correct roll no.");
}

关于mysql - 如何仅使用 Jdbc 从 MySQL 验证后检索特定用户的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683588/

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