无法从我的sql中检索数据到java eclipse

时间:2014-12-05 15:40:27

标签: java mysql

我无法将数据从mysql检索到java eclipse控制台。如何从mysql中检索和显示数据。

DB TABLE列:Student_ID | Student_FirstName | Student_LastName | Student_EmailID | Student_Course

以下是代码:

package jdbcdemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Scanner;
import java.sql.ResultSet;  
import com.mysql.jdbc.Statement;

public class StudentDetail {

    public static void studentinfo() {

        System.out.println("Enter the stdentid to view details");

        Scanner studentid= new Scanner(System.in);
        int stdnumber = studentid.nextInt();
        studentid.nextLine();

        //Connection to db
        try{
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_db","root","admin");
        Statement stmt = (Statement) conn.createStatement();
        String details = "SELECT * FROM `student_db`.`studentinfo` WHERE `Student_ID`='"+stdnumber+"'";
        ResultSet rs= stmt.executeQuery(details);

        }catch (Exception e){
            System.err.println(e);
        }

    }

}

输出:没有显示错误和" EXIT"显示为输出。

1 个答案:

答案 0 :(得分:0)

查询表(您已完成)后,需要从ResultSet中提取数据。 E.g:

ResultSet rs= stmt.executeQuery(details);
while (rs.next()) {
      System.out.println ("Student ID: " + rs.getString(1);
      System.out.println ("Student name: " + rs.getString(2);
}