GeeCON Prague 2014*
The moose likes JDBC and the fly likes JDBC and mySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC and mySQL" Watch "JDBC and mySQL" New topic
Author

JDBC and mySQL

Alex Wood
Greenhorn

Joined: Aug 15, 2001
Posts: 3
I am trying to create a serverlet that will take a HTML form and place the information into a mySQL database. My current attempt seems to have a few problems. Any idea on how to correct it?
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.util.*;
public class formReader extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response){
String url = "jdbc:mysql://197.100.0.191:3306/project";
Connection con;
Statement stmt;
String fName = "";
String lName ="";
String emailAddress ="";
String idnumber = "";
String pWord ="";
{
try { fName = request.getParameter("firstName"); } catch (Exception e) { e.printStackTrace();}
try { lName = request.getParameter("lastName"); } catch (Exception e) { e.printStackTrace();}
try { emailAddress = request.getParameter("eMail"); } catch (Exception e) { e.printStackTrace();}
try { idnumber = request.getParameter("idNumber"); } catch (Exception e) { e.printStackTrace();}
try { pWord = request.getParameter("passWord"); } catch (Exception e) {e.printStackTrace();}

try {
Class.forName("mm.mysql.-2.0.4-bin");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
stmt.executeUpdate(
"INSERT INTO PROJECT" +
"VALUES(fName, lName, emailAddress, idnumber, pWord)");
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
}
Any help will be welcome.
Thanks.
Robert Brunner
Ranch Hand

Joined: Jul 18, 2001
Posts: 49
Could you post more information about the problem you are having?
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Which part of the code went wrong? Can you post the error too? If you like, you can try Class.forName("org.gjt.mm.mysql.Driver").newInstance() . I have a ready to run code for you with it.
[This message has been edited by SoonAnn Lim (edited August 15, 2001).]
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Also you need to give the username and password. Here is my working example
------------------------------------------------------------
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Mysqltest extends HttpServlet {
Connection con=null;
Statement stmt=null;
String Driver="org.gjt.mm.mysql.Driver";

public void init (ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName(Driver).newInstance();
System.out.println("Driver success");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/News?user=root&password=rootpassword");
System.out.println("Connection success");
}
catch ( ClassNotFoundException e) {
throw new UnavailableException(this, "Couldn't load JdbcMysqldriver");
}
catch (Exception e) {
throw new UnavailableException(this, "Couldn't get db connection");

}
//catch (InstantiationException e){}
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ResultSet rs=null;
try{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
stmt=con.createStatement();
rs = stmt.executeQuery("Select id, author, topic,content,post_dt from local");
out.println("<html><head><title>mysqltest</title></head><body>");
out.println("
    ");
    while(rs.next()){
    out.println("
  • "+rs.getInt(1));
    out.println("
  • "+rs.getString(2));
    out.println("
  • "+rs.getString(3));
    out.println("
  • "+rs.getString(4));
    out.println("
  • "+rs.getDate(5));
    }
    out.println("
  • </body></html>");

    }
    catch (SQLException e){
    System.out.println("Error");
    }
    }
    }
    --------------------------------------------------------------
    Hope this help.
Raghav
Greenhorn

Joined: Jul 24, 2001
Posts: 14
Hi,
1.)I think the first error lies in you Class.forName() line of code . it must be
Class.forName("org.gjt.mm.mysql.Driver");
2.) I think the insert statement u are using must be like st.executeUpdate("INSERT INTO PROJECT VALUES('"+fName+"', '"+lName+"', '"+emailAddress+"', '"+idnumber+"', '"+pWord+"')");
3. try giving the connection like
DriverManager.getConnection(url,"","");
try changing all these see if it works if u can post the error too that will be fine .
regards
raghav
 
GeeCON Prague 2014
 
subject: JDBC and mySQL