如何从表中获取信息并使用java将其显示为JTeaxtarea

时间:2015-08-07 14:32:20

标签: java

请我需要获取信息并将它们存储在jtextarea中请帮忙 这是我的代码

try {
    //get connection to the database
    Connection myconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cbt_for_java", "root","");
    //create a statement
    Statement mystmt=myconn.createStatement();
    //execute sql query
    ResultSet myrs = mystmt.executeQuery("select * from  jamb WHERE ID = '1'");
    //process the result set

    //System.out.println(myrs.getString("ID") + "." + myrs.getString("question"));

    //q=myrs.getString("ID") + "." + myrs.getString("question");     
  } catch(Exception exc) {
      exc.printStackTrace();
  }
  BorderLayout questionareaLayout = new BorderLayout();
  JPanel questionp = new JPanel();
  JTextArea question=new JTextArea(22,111);
  question.setEditable(false);
  question.setText(System.out.println(myrs.getString("ID") + "." + myrs.getString("question"));
  questionp.add(question);
  add(questionp); 

2 个答案:

答案 0 :(得分:0)

你的问题在这里:

question.setText(System.out.println(myrs.getString("ID") + "." + myrs.getString("question"));

JTextArea' setText()方法接受String作为参数。 System.out.println() 会返回字符串! 只是 打印您将其提供给控制台的字符串。所以,解决它:

question.setText(myrs.getString("ID") + "." + myrs.getString("question"));

答案 1 :(得分:0)

while(myrs.next()) {
   question.setText(question.getText() + myrs.getString("id") + ": " + myrs.getString("question")); //value in getString depends on your table.
}

如果您想分开结果,例如使用换行符,只需在两个字符串之间添加“\ n”。