2017-04-08 49 views
0

JDBC-MySQL驅動程序已正確安裝並且連接完全正常工作。JSP/MySQL(JDBC) - Select語句給出空結果

我已經插入了創建的數據庫,表,插入行和從視圖數據我已經執行了選擇語句。那些工作正常。但是在這段代碼中,只有INSERT聲明正在工作並且加入結果SELECT聲明不是。 在互聯網上搜索幾小時後,我也沒有線索。

這裏是代碼 -

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<% 

    String connectionURL = "jdbc:mysql://localhost:3306/users"; 
    Connection connection = null; 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection(connectionURL, "root", "pass"); 
    if (!connection.isClosed()) { 
     out.println("Connection Established"); 

     PreparedStatement preparedStmt = connection.prepareStatement("INSERT INTO users.user_details (`Email`, `Name`, `Password`) VALUES ('[email protected]', 'Ram Das', '12345678')"); 
     preparedStmt.execute(); 
     String sql="select * from user_details"; 
     PreparedStatement statement=connection.prepareStatement(sql); 
     ResultSet rs=statement.executeQuery(); 
     if(rs.next()) 
     { 
      System.out.println(rs.getString(1)); 
     } 
    } 
    connection.close(); 
%> 

enter image description here

enter image description here

+2

你用'System.out'代替了。 ist打印在控制檯中? – SilverNak

+0

@SilverNak,哈哈,謝謝,我只是忽略了它。我的錯。 – RupamDotInfo

回答

1
While(rs.next()) 
    { 
     System.out.println(rs.getString("Email")); 
    } 

使用時間,而不是如果。

如果您正在使用通過out.println(),你會看到在瀏覽器中輸出,如果你正在使用System.out.println(),你會看到在IDE的輸出窗口輸出。

+0

是的,我現在明白了。並提醒+1使用「while」,因爲在「if」的情況下,只會顯示一個結果。 – RupamDotInfo