JDBC ResultSet no results?
Why aren't there any results for this query? Only "Movies!" is printed when i run this servlet.
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class Service extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Movies!");
Connection connection = null;
Statement statement = 开发者_如何转开发null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
statement = connection.createStatement();
String query = "SELECT * FROM movies";
ResultSet rs = statement.executeQuery(query);
while(rs.next()) {
out.println("result set");
out.print(rs.getInt(1));
out.print(rs.getString(2));
out.print(rs.getInt(3));
out.print(rs.getInt(4));
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
Best guess: There are no rows in the table. What's the result when you execute the query manually ?
mysql test # connect to mysql
SELECT * FROM movies;
You're writing exceptions to server log file by e.printStackTrace()
instead of throwing it so that it would end up in a nice error page in webbrowser.
Fix it as follows:
throw new ServletException("Querying from DB failed!", e);
Otherwise you've got to dig in the server logs for the exact cause of the problem.
Unrelated to the actual problem, remember to close your resources properly in a finally
block. You're leaking them. See also the basic JDBC tutorial.
Update: finally, you got the cause of the problem:
java.sql.SQLException: No suitable driver found
You need to download the JDBC driver, put the JAR file in /WEB-INF/lib
folder and load it in your code as follows before you acquire any connection:
Class.forName("com.mysql.jdbc.Driver");
See also:
- Java connectivity with MySQL - mini tutorial
Download MySQL JDBC Driver from
http://dev.mysql.com/downloads/connector/j/
Copy jar into WEB-INF/lib folder
hi before compilation you must put the clsses12.jar or other Ojdc14 jar file put in class path then make compile . some sample code here ResultSet res=statement.executeQuery("SELECT FIRST-NAME FROM FOO"); StringBuffer sb=new StringBuffer(); while(rs.next()) { sb.append("\n"+rs.getString(1)); }
Hi Download MYSQL JDBC driver from http://www.mysql.com/products/connector/ under the like JDBC Driver for MYSQL. place jdbc jar file at location - /WEB-INF/lib - in your application.
In your code following statement is missing
Class.forName("com.mysql.jdbc.Driver");
Place it before your following line of code
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
And restart the server..Make sure your database service is running...
And check..
I hope you would not get the error again...
~ Priyanjan
精彩评论