Results 1 to 7 of 7
- 02-17-2008, 05:48 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
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
- 03-02-2008, 06:56 AM #2
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
Maybe you can try to change the class driver for this one and see whether it can solve the problem:
Class.forName("org.gjt.mm.mysql.Driver");
If still doesn't work, you can try to set the jdbc using the ip address instead of localhost and see whether it works or not.
- 03-26-2008, 11:47 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 13
- Rep Power
- 0
where u r doing the program actually from console or Eclipse editor
If its from console may be the calss path connection
u just verify classpath is set corectly or not to u realted jar file
- 04-02-2008, 08:40 AM #4
Member
- Join Date
- Mar 2008
- Posts
- 13
- Rep Power
- 0
connection from servlet to database
Me also getting the same problem
Did u get the solution how u relove u r problem
Please tell me how did u solve that
- 04-02-2008, 11:16 AM #5
Have you checked the sqljdbc.jar (JDBC driver) is set correctly in the classpath??
Did this post help you? Pleaseme! :cool:
- 04-02-2008, 11:32 AM #6
Member
- Join Date
- Mar 2008
- Posts
- 13
- Rep Power
- 0
I sovel my problem
thnQ for the reply
I solved my problem some one suggets add the ojdbc14.jar file into web-inf\lib directory then i got the ans correctly
- 04-02-2008, 11:33 AM #7
Similar Threads
-
connecting to a database
By mxb7642 in forum JDBCReplies: 5Last Post: 09-08-2009, 08:39 AM -
Connecting to a database
By peiceonly in forum New To JavaReplies: 2Last Post: 04-06-2008, 02:28 PM -
Problem with jTable that is binded with a table in MySQL Database
By rajkenneth in forum NetBeansReplies: 0Last Post: 03-29-2008, 03:36 PM -
Problem connecting to my Database using Java
By javaneed in forum New To JavaReplies: 3Last Post: 08-13-2007, 09:35 AM -
connecting to mysql database
By javagal in forum NetBeansReplies: 2Last Post: 08-04-2007, 12:36 PM
Bookmarks