2013-04-02 199 views
0
Connection con = null; 
Statement stmt = null; 
Statement resultStmt = null; 
ResultSet rs = null; 
try { 
    // load database driver driver 
    System.out.println("Database driver is: " + DataSource.getClassName()); 
    Class.forName(DataSource.getClassName()); 

    // connect to database from a given URL with a given username and password 
    System.out.println("Database URL is: " + DataSource.getURL()); 
    con = DriverManager.getConnection(DataSource.getURL(), DataSource.getUserName(), DataSource.getPassword()); 

    // create an SQL statement object 
    stmt = con.createStatement(); 

    stmt.executeUpdate("INSERT INTO leadcustomer " + "VALUES(1, 'junwei', 'Li', 'heaven road','[email protected]')"); 

    String SQLStatement = "SELECT * FROM leadcustomer"; 
    System.out.println("Q1 SQL Statement is: " + SQLStatement); 

    rs = resultStmt.executeQuery(SQLStatement); 

    while (rs.next()) { 
     int customerid = rs.getInt("customerid"); 
     String fistname = rs.getString("firstname"); 
     String surname = rs.getString("surname"); 
     String billAddress = rs.getString("billingAddress"); 
     String email = rs.getString("email"); 

     System.out.println("customerid : " + customerid); 
     System.out.println("firstname : " + fistname); 
     System.out.println("surname : " + surname); 
     System.out.println("billingAddress : " + billAddress); 
     System.out.println("email : " + email); 


     System.out.println(customerid + " : " + fistname + "--" + surname + "--" + billAddress + ":" + email); 
    } 

    con.close(); 

    // extract name from first row and print 

} catch (SQLException e) { 
    // print details of SQL error 
    // could be multiple errors chained together 
    System.err.println("Error(s) occurred"); 
    while (e != null) { 
     System.err.println("SQLException : " + e.getMessage()); 
     System.err.println("SQLState : " + e.getSQLState()); 
     System.err.println("SQLCode : " + e.getErrorCode()); 
     e = e.getNextException(); 
     System.err.println(); 
    } 
} 

我試圖插入數據並在插入後選擇表。但它返回錯誤消息「沒有結果被查詢返回」查詢返回沒有結果

我確實使用了executeUpdateexecuteQuery來查看不同的SQL語句。

對此有何建議?

順便說一句,插入操作正在運行成功。 我唯一想要的只是解決出錯並執行select語句打印出表格。

回答

1

您的resultStmt尚未初始化。添加

resultStmt = con.createStatement(); 

rs = resultStmt.executeQuery(SQLStatement); 
+0

ya..problem解決我張貼後......我還沒有清理並生成項目,我在程序運行前.......這麼愚蠢。 ....:p謝謝你的回覆.. – johnnily