Reputation: 513
i am trying to get stored procedure datas from calling database through JDBC.Here i am getting resultset=null when i execute below code,what is the mistake in my code?Please guide me.
CREATE PROCEDURE getEmpDetails
AS
BEGIN
SELECT * FROM Employees;
END
java file
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL = "jdbc:sqlserver://localhost;database=JDBCDatabase";
// Database credentials
static final String USER = "hj";
static final String PASS = "kalpana";
public static void main(String[] args) {
Connection con = null;
ResultSet rs = null;
CallableStatement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
con = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
// stmt = conn.createStatement();
stmt = con.prepareCall("{call getEmpDetails}");
ResultSet resultSet = stmt.getResultSet();
System.out.println("resultset"+resultSet);
if (resultSet.next()) {
// Then we use a loop to retrieve rows and column data
// and creates a html coded table output
System.out.println("<table border='1' >");
do {
System.out.println("<tr>");
System.out.print("<td>" + resultSet.getString("id") + "</td>");
System.out.print("<td>" + resultSet.getString("first") + "</td>");
System.out.println("<td>" + resultSet.getInt("last") + "</td>");
System.out.println("<td>" + resultSet.getDouble("age") + "</td>");
System.out.println("</tr>");
} while (resultSet.next());
System.out.println("</table>");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
con.close();
// input.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 0
Views: 2890
Reputation: 13858
In your code your prepare the call, and then you go and expect Results - but without executing the query. Try changing the accesor on the ResultSet to the actual execution of your query:
stmt = con.prepareCall("{call getEmpDetails}");
//Do it!
ResultSet resultSet = stmt.executeQuery();
Upvotes: 1