1

Hi i have the following file to connect mysql database to html files. But i am having trouble connecting it. Can anyone tell me where i find the locations. What should i replace "jdbc:mysql://localhost/zulfiqar" with for it to work on my computer? where do i find this? And is there anything else i have to change to make it work on my computer? this was a piece of code i found on the internet which i am trying to make work so i can understand how to do it, but i am struggling. Thanks in advance!

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUserEnquiryForm extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
/**Process the HTTP Get request*/
public void doPost(HttpServletRequest req, 
HttpServletResponse res) throws ServletException,
IOException{
String connectionURL = "C:\Program Files(x86)\MySQL Server 5.0\bin\mysql.exe";
Connection connection=null;
ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//get the variables entered in the form
String uId = req.getParameter("userId");
String fname = req.getParameter("firstname");
String sname = req.getParameter("surname");
String address1 = req.getParameter("address1");
String address2 = req.getParameter("address2");
String town = req.getParameter("town");
String county = req.getParameter("country");
String zipcode = req.getParameter("zipcode"); 
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection
(connectionURL, "root", "admin"); 
//Add the data into the database
String sql = 
"insert into emp_details values (?,?,?,?,?,?,?,?)";
PreparedStatement pst = 
connection.prepareStatement(sql);
pst.setString(1, uId);
pst.setString(2, fname);
pst.setString(3, sname);
pst.setString(4, address1);
pst.setString(5, address2);
pst.setString(6, town);
pst.setString(7, county);
pst.setString(8, zipcode);
int numRowsChanged = pst.executeUpdate();
// show that the new account has been created
out.println(" Hello : ");
out.println(" '"+fname+"'");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " 
+ e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " 
+ e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
user1162494
  • 43
  • 1
  • 4
  • 10

2 Answers2

0

Some additional information about how it's failing might be useful.

1) Is it failing to make the socket connection (implying your service isn't running), or 2) Did it fail to initialize the driver? I'm not familiar with the one you listed. A more common alternative is "sun.jdbc.odbc.JdbcOdbcDriver". 3) Did you connect and simply fail authentication with user "root" and password "admin"?

Raevik
  • 1,945
  • 9
  • 32
  • 53
  • Im not so sure if it's making a connection. tbh im really new to this and am confused on how to set the whole thing up. – user1162494 Mar 01 '12 at 20:20
  • org.gjt.mm.mysql.Driver and com.mysql.jdbc.Driver are same. see here http://stackoverflow.com/a/5808184/778687 – tusar Mar 08 '12 at 06:08
0

What should i replace "jdbc:mysql://localhost/zulfiqar" with

Ans: It is the connection url. It does mean your MySQL database is running on localhost server (with default port) and you are connecting to 'zulfiqar' database. So first line under doPost() should be :

String connectionURL = "jdbc:mysql://localhost/zulfiqar";

Next, you are using org.gjt.mm.mysql.Driver driver for JDBC connection. It was initially developed by a hobbyist. It's later donated to MySQL where they renamed the package/classname. The old classname is kept for backwards compatibility reasons, but you should update it to com.mysql.jdbc.Driver and add mysql-connector-java-*-bin.jar in your WEB-INF/lib folder.

Next thing you are using :

connection = DriverManager.getConnection(connectionURL, "root", "admin");

So you are loading a connection from the connectionURL and accessing it with root user and admin password. Make sure these are correct in your case.

Last point is, you are inserting into emp_details table. Make sure you have this table already created in zulfiqar database with all required columns. And the number of '?' marks in the sql string should match the number of times you are doing pst.setString(index, data), otherwise you will get Invalid parameter index error.

tusar
  • 3,364
  • 6
  • 37
  • 60