|
problem in connecting to mysql database
Hi all
I tried to run the following code..to connect to the database but getting Exception: com.mysql.jdbc.Driver....
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class dataservlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int emid=0;
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
String url = "jdbc:mysql://localhost/shipment";
String query = "SELECT user_id FROM tbl_user";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance ();
Connection con = DriverManager.getConnection(url,"nancy","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery (query);
while(rs.next())
{
emid=rs.getInt("user_id");
}
out.println(emid);
rs.close();
stmt.close();
con.close();
}
catch (SQLException ex) {
out.println ("SQL Exception: " + ex.getMessage());
}
catch (java.lang.Exception ex) {
resp.setContentType("text/html");
out.println ("Exception: " + ex.getMessage ());
}
}
}
I cannot connect to the database at all..I have created the database and tables correctly...Please help in finding my mistake...Thank you
|