mysql connection .....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitrohitrohit
    New Member
    • Oct 2007
    • 18

    mysql connection .....

    Hi every one
    i am a beginner....... ...
    i installed xampp in my computer.(php+m ysql) i wrritten a code below.
    but it generate a error class not found.

    can i connect my servlets with mysql which i installed with xampp?


    //import javax.sql.*;
    import java.sql.*;
    import javax.servlet.* ;
    import javax.servlet.h ttp.*;
    import java.io.*;

    public class Registration extends HttpServlet
    {
    private ServletContext application;
    public void init(ServletCon fig config) throws ServletExceptio n
    {
    super.init(conf ig);
    application= config.getServl etContext();
    }
    public void doPost(HttpServ letRequest req , HttpServletResp onse res) throws ServletExceptio n,IOException
    {


    try
    {
    Connection conn = null;
    String url = "jdbc:mysql ://localhost:3306/";
    String dbName = "mis";
    String driver = "com.mysql.jdbc .Driver";
    String userName = "root";
    String pass = "";

    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    Connection con=DriverManag er.getConnectio n(url+dbName,us erName,password );
    PreparedStateme nt pstat=con.prepa reStatement("in sert into registration values(?,?)");

    String firstname=req.g etParameter("us ername");
    String password=req.ge tParameter("pas sword");

    pstat.setString (1,firstname);

    pstat.setString (2,password);


    int inser=pstat.exe cuteUpdate();
    if(inser==1)
    {
    res.setContentT ype("text/html");
    PrintWriter out=res.getWrit er();
    out.println("<h tml>");
    out.println("<b ody>");
    out.println("<b >Please&nbsp;Wa it.......</b>");
    out.println("</body>");
    out.println("</html>");
    res.setHeader(" Refresh","5;URL = http://localhost:8000/musicsworld/jsp/RegistrationSuc cess.jsp");

    // The code i written below works well but after disscussion i am going to display this result through jsp
    /* res.setContentT ype("text/html");
    PrintWriter out=res.getWrit er();
    out.println("<h tml>");
    out.println("<h ead>");
    out.println("<t itle>www.Musics Inc.com</title>");
    out.println("</head>");
    out.println("<b ody bgcolor=Tan>");
    out.println("<i mg src=\"C:/j2sdkee1.2.1/public_html/musicsworld/img/rohit.jpg\">");
    out.println("<b r>");
    out.println("<f ont face=arial color=#cc3333>T hank you for Registration in our web site</font>");
    out.println("<p align=Left><b>< font face=arial color=#cc3333 size=6>Congratu lation !&nbsp; on Successful Registration... ..</font></b></p>");
    out.println("<h 3><font face=arial color=red>This site is Greate for fun and Masti.&nbsp;&nb sp;We hope You will Enjoy with it.&nbsp;Please Remember Your Id and password for Login.Our Private Policy Terms $ Condition Restrices Unauthories access of any Account.</h3>");
    out.println("<h 3><font face=arial color=red>Here your Id will your Name which you entered in Registration Form.</h3>");
    out.println("<h r height= width= color=gray>");
    out.println("</body>");
    out.println("</html>"); */


    }
    }
    catch(Exception e)
    {




    res.setContentT ype("text/html");
    PrintWriter out=res.getWrit er();

    out.println("<h tml>");
    out.println("<h ead>");
    out.println("<t itle>www.Musics Inc.com</title>");
    out.println("</head>");
    out.println("<b ody bgcolor=Tan>");
    out.println("<b >Your Request Failed Transaction Has Not Done</b>");
    out.println("<b r>");
    out.println("<b >Error in connectivity... ............</b>");
    out.println(e);
    out.println("<b r>");
    out.println("<b >Please Carefully Fillup Your Registration Form&nbsp;&nbsp ;Filling Values Must Match As Accordidng TO Requirements... </b>");
    out.println("<b r>");
    out.println("Re mote Address: " + req.getRemoteAd dr());
    out.println("<b r>");
    out.println("Pa thInfo: " + req.getPathInfo ());
    out.println("<b r>");
    out.println("Pr otocol: " + req.getProtocol ());
    out.println("<b r>");
    out.println("Pr otocol: " + req.getAuthType ());

    out.println("<b r><br><br><br>" );
    out.println("<h r height=600 width=400 color=gray>");
    out.println("</body>");
    out.println("</html>");



    }
    }
    }
    Last edited by rohitrohitrohit; Feb 11 '08, 07:30 AM. Reason: adding some line in post
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code
    2.) What's the exact exception that you got?
    3.) Why don't you use MySQL's JDBC driver for the connection. Download it from the MySQL site.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by rohitrohitrohit
      String driver = "com.mysql.jdbc .Driver";
      ...
      Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
      Do you see that you don't use driver?

      Also, the most important thing you should learn how to do is separate unrelated layers of code: your servlets/JSP should not have any mention of database access.

      Get your database code working first, in simple example programs.

      Comment

      Working...