问题
Am developing an application to query the table and print it out in the JTextarea but not responding instead flagging an error 'illegal start of expression' please am new with i need help and this is my code thanks
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"));
}
catch(Exception exc){
exc.printStackTrace();
}
BorderLayout questionareaLayout = new BorderLayout();
JPanel questionp = new JPanel();
JTextArea question=new
//this is where the problem is
JTextArea(System.out.println(myrs.getString("ID") + "." + myrs.getString("question"));,22,111);
question.setEditable(false);
questionp.add(question);
add(questionp);
回答1:
System.out.println() does have a return value, thus you cannot use it to set a value. What it does is write to the console.
To set a value in a textfield use the setText(), without the System.out.println
回答2:
Instead of the complex expression (that will not serve as a correct argument) passed as to the constructor of the JTextArea
System.out.println(myrs.getString("ID") + "." + myrs.getString("question"));,22,111
Try creating a String first, then passing that in as a parameter, like so:
String text = "id" + "text" + "stuff";
JTextArea question = new JTextArea(text);
回答3:
System.out.println will print out to the command line you cant use it to set the text in a JTextArea you need to do:
JTextArea question = new JTextArea(22,111);
question.setText(myrs.getString("ID") + "." + myrs.getString("question"));
回答4:
String s=myrs.getString("ID") + "." + myrs.getString("question");
here you will get your string in s now pass the string with along with other parameter.
JTextArea(s,22,111);
来源:https://stackoverflow.com/questions/31873037/using-system-out-print-in-a-texarea