我正在尝试将mysql查询结果打印到jLabel中

时间:2014-03-13 13:45:37

标签: java mysql swing formatting jlabel

因此,当我单击另一个“show hint”标签时,查询语句本身将被打印到jLabel中而不是将查询结果打印到Jlabel中

private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {

try
    {            
    Class.forName("com.mysql.jdbc.Driver");            
    Connection CC = DriverManager.getConnection(
           "jdbc:mysql://localhost:3306/pulsedb", "root", "passtheword");
    String query = "SELECT hint FROM mstufflogin WHERE username = 'tapiwa'";            
    PreparedStatement PS = CC.prepareStatement(query);            
    PS.executeQuery(query);            
    jLabel6.setText("HINT: " + query);
}
标签上的

输出将是.... SELECT hint FROM mstufflogin WHERE username = 'tapiwa'

2 个答案:

答案 0 :(得分:1)

将查询替换为查询结果

ResultSet result = PS.executeQuery();
result.next();
jLabel6.setText("HINT: " + result.getString(1));

答案 1 :(得分:-1)

我认为你选错了变量(查询)?

尝试

jLabel6.setText("Hint:"+PS.getString("HINT"));