当我尝试从数据库mysql显示数据(查询结果)到jTextarea时出现问题,我在编译时遇到如下错误异常:
SQL Exception: java.sql.SQLException: Can not issue SELECT via executeUpdate()
我从表中使用了一个“选择”查询,其中的名称是写在我的jTextFieldNom中的名称,这是我的代码,我希望有人能帮到我,因为我不知道如何解决该问题,我敢肯定我的查询是正确的,但我不知道问题出在哪里。
String pilote = "com.mysql.jdbc.Driver";
jComboBoxType.addItemListener(new ItemState());
jComboBoxMenaces.addItemListener(new ItemState());
try {
Class.forName(pilote);
Connection connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root"," ");
Statement instruction = connexion.createStatement();
String a=jTextFieldNom.getText();
String SQL = "select description from table where nomcol="+a+";";
ResultSet rs = instruction.executeQuery(SQL);
instruction = connexion.createStatement();
int rowsEffected = instruction.executeUpdate(SQL);
jTextArea1.append(rs.getString("description"));
}
...... //bloc catch
最佳答案
该行正在执行引发错误的Select语句。
int rowsEffected = instruction.executeUpdate(SQL);
您不需要此行,因为您没有更新数据库。
同时将追加更改为setText
jTextArea1.setText(rs.getString("description"));
尝试这个:
String pilote = "com.mysql.jdbc.Driver";
jComboBoxType.addItemListener(new ItemState());
jComboBoxMenaces.addItemListener(new ItemState());
try {
Class.forName(pilote);
Connection connexion = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root"," ");
Statement instruction = connexion.createStatement();
String a=jTextFieldNom.getText();
String SQL = "select description from table where nomcol="+a+";";
ResultSet rs = instruction.executeQuery(SQL);
jTextArea1.setText(rs.getString("description"));
}
关于mysql - 如何从数据库Mysql将数据显示到Jtextarea中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10076397/