MySQL Forums
Forum List  »  Microsoft SQL Server

Re: Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql:// localhost:3306/karthicraj","mysql","mysql");
Posted by: karthicraj rajendran
Date: September 10, 2009 11:15AM

Dear friend

first you add MySql Jar file .
Steps in My Eclips 5.01
1- Right click in your project
2-Select Built Path
3- then selct Add External Archive
4- D:\Program Files\MySQL\MySQL Tools for 5.0\java\lib
select jar file..
finish now you may add jar file...

Check this in ur code:-

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql");

then u run it will give result

Same code:
----------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Connection.*;


public class EmpDetail extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
Connection con;
Statement stm;
PreparedStatement pstm;



String s1=request.getParameter("s1");
String s2=request.getParameter("s2");
String s3=request.getParameter("s3");

PrintWriter pw=response.getWriter();



String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
int salary=Integer.parseInt(request.getParameter("salary"));

pw.println("<HTML>");
pw.println("<BODY>");



if(s1!=null)
{

try
{


Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql");
System.out.println ("Database connection established");
System.out.println("hi");
stm=con.createStatement();
String strQry="Insert into ram(name,age,salary)values(?,?,?)";
pstm=con.prepareStatement("Insert into ram(name,age,salary)values(?,?,?)");
System.out.println("Ready for Insert");
pstm.setString(1,name);
pstm.setInt(2,age);
pstm.setInt(3,salary);
pstm.executeUpdate();
System.out.println("Updated");
pstm.close();
con.close();

pw.println("thank u for Inserting data");
RequestDispatcher dispatcher = request.getRequestDispatcher("EmpDetail.html");
dispatcher.include(request, response);
}
catch(ClassNotFoundException e)
{
pw.println(e);
}
catch(SQLException e)
{
pw.println(e);
}

}

else if(s2!=null)
{

try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql");
System.out.println("connection success 1");
stm=con.createStatement();

String strQry="update ram set age=?,salary=? where name=? "; // and age=?";

pstm=con.prepareStatement(strQry);

pstm.setInt(1,age);
pstm.setInt(2,salary);

pstm.setString(3,name);
//pstm.setInt(4,age);

pstm.executeUpdate();

pstm.close();
con.close();

pw.println("thank u for updating data");
RequestDispatcher dispatcher = request.getRequestDispatcher("EmpDetail.html");
dispatcher.include(request, response);

}
catch(ClassNotFoundException e)
{
pw.println(e);
}
catch(SQLException e)
{
pw.println(e);
}

}

else
{

try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql");
stm=con.createStatement();



stm.executeUpdate("delete from ram where name='"+name+"' and age="+age);


stm.close();
con.close();
pw.println("thank u for Deleting data");
RequestDispatcher dispatcher = request.getRequestDispatcher("EmpDetail.html");
dispatcher.include(request, response);

}
catch(ClassNotFoundException e)
{
pw.println(e);
}
catch(SQLException e)
{
pw.println(e);
}

pw.println("</BODY>");
pw.println("</HTML>");

}


}
}

Options: ReplyQuote


Subject
Written By
Posted
Re: Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql:// localhost:3306/karthicraj","mysql","mysql");
September 10, 2009 11:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.