1

当我尝试将数据(查询结果)从我的数据库 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
4

1 回答 1

0

此行正在执行引发错误的 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"));                                     
}
于 2012-04-09T19:31:27.827 回答