这是一个代码片段.......我只是希望它在成功登录验证该用户后检索用户的信息.....(我只使用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();**
}
}
答案 0 :(得分:0)
如前所述,rs与roll完全不同。 rs表示包含对查询的所有响应的ResulSet,您已发送到db。 由于您已在声明中输入了卷号,我认为卷号是唯一的,您的代码可能如下所示:
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.");
}