i am writing code and also the error i am facing in database connectivity in jsp to mysql .plz help me .thanx

code

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.*;
import java.io.*;

public class ConnBean 
{
Connection conn;
Statement st ;
  public ConnBean()
  {
     String sqlDriver= "com.mysql.jdbc.Driver";
     String hostName    = "localhost:3306/";
     String dbName  = "mydb";
     String userID  = "root";
      String password   = "webportal";

    //databse URL
    String dbURL = "jdbc:mysql://" + hostName +  dbName;

    try {
      Class.forName(sqlDriver);
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(dbURL, userID, password);
      st = conn.createStatement();
      System.out.println("db connected");
    }
    catch (SQLException e) {

      System.out.println("Exception occured during get connection" + e);
    }
  }

public static void main(String asdas[]) throws SQLException
  {

  ConnBean conn=new ConnBean();


 //ResultSet rs=conn.executeQ("select * from clients");
  }
}

and the error that jdk 1.5 ( with mysql 5.0.41) is showing is

C:\Program Files\Java\jdk1.5.0\bin\java.exe   ConnBean
Working Directory - D:\jsp code\
Class Path - .;c:\Kawapro5.0\kawaclasses.zip;c:\program files\java\jdk1.5.0\lib\tools.jar;c:\program files\java\jdk1.5.0\jre\lib\rt.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Class.java:164)

    at ConnBean.<init>(ConnBean.java:25)

    at ConnBean.main(ConnBean.java:44)

Exception occured during get connectionjava.sql.SQLException: No suitable driver

Process Exit...

Make sure you have "com.mysql.jdbc.Driver" in your web application. It will be contained in a .jar and it needs to go with your other java libraries you use.

please tell me which .jar file has to saved and where in jdk or in tomcat althought i have mysql-connector-java-3.0.8-stable-bin.jar file that i have copied in tomcat in libs .please tel are u talking about this .jar file now wat i have to do?please guide me kindly

I don't know what .jar the mysql driver is in. It shouldn't be that hard to find with a google. The .jar file goes into your web app directory under "WEB-INF\lib".

Copy that jar file into WEB-INF/lib, not into just any generic tomcat lib directory.

Copy that jar file into WEB-INF/lib, not into just any generic tomcat lib directory.

yes i have copied the .jar files in WEB-INF/lib but still when i have compiled database connectivity program with mysql in java it gave the same error.

D:\Program Files\Java\jdk1.5.0\bin\java.exe ConnBean
Working Directory - C:\final project\jsp code\
Class Path - .;c:\Kawapro5.0\kawaclasses.zip;d:\program files\java\jdk1.5.0\lib\tools.jar;d:\program files\java\jdk1.5.0\jre\lib\rt.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:164)

at ConnBean.<init>(ConnBean.java:25)

at ConnBean.main(ConnBean.java:44)

Exception occured during get connectionjava.sql.SQLException: No suitable driver

Process Exit...

Is there any need to copy the .jar files(that are mysql-connector-java-3.0.8-stable-bin) in jdk.If so then where these files should be copied in order to include in the class path of java.please answer it to solve my problme.thanx

I have the same problem. Try to copy MySQL connector jar file to
%JAVA_HOME%/jre/lib/ext/

I have the same problem. Try to copy MySQL connector jar file to
%JAVA_HOME%/jre/lib/ext/

Bad, bad, bad. Never copy anything to the lib/ext directory of the jre. Learn how to use and set classpaths properly. If he has the proper jarfile, and places it in the WEB-INF/lib directory of the web application he is trying to reach, the classes will be found. If they are not, he has done something wrong. Either copied it to the lib directory of the wrong application, not copied the right jarfile, or, maybe, somehow or another, changed the tomcat configuration so that it does not look there. Sorry to say it, but all of those things are "his problem", and copying anything to the lib/ext directory is not the answer, regardless of whether or not "it works".

commented: I agree. "Never copy anything to the lib/ext directory" +5

yes i have copied the .jar files in WEB-INF/lib but still when i have compiled database connectivity program with mysql in java it gave the same error.

D:\Program Files\Java\jdk1.5.0\bin\java.exe ConnBean
Working Directory - C:\final project\jsp code\
Class Path - .;c:\Kawapro5.0\kawaclasses.zip;d:\program files\java\jdk1.5.0\lib\tools.jar;d:\program files\java\jdk1.5.0\jre\lib\rt.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

....

Why are you trying to run the ConnBean from the jdk bin folder if you wish to access this through Tomcat? If you want to run from the command line, you need to specify the classpath. Many posts have told you how to do that. If you have a JSP/servlet app running in Tomcat that needs that ConnBean, then placing the jar in WEB-INF/lib for that app is all that you need to do.

2masijade I beginner in java too. So sometimes copy jars to jre folder (when project not compiled and i am angry :) )

In attachment sent sample project for test MySQL connection. Maybe it helps TS to solve his problem.

2masijade I beginner in java too. So sometimes copy jars to jre folder (when project not compiled and i am angry :) )

And as a quick fix to get something compiled or run for the first time when you are frustated, and it is not meant as a solution, but rather just a quick stopgap, I don't object to it that much (although it still should not be done, it shouldn't get to that point, but sh1t happens sometimes). The real problem comes in (at least the point where I get upset about it) is when I see someone suggest that as the solution for a problem.

It always start with quick fix, but in no time you find that quick fix project is project on which you work for weeks/months. You submit to your teacher/employer/customer to find "suddenly" it does not work for them.

So as masijade and others said it is always best to do it proper way.

I think for a beginner important to make the project efficient. And then to understand details. If the program does not work - that to test there is nothing. Though I too for the serious approach in development of commercial projects. But adjustment connector for a DB is not so critical in comparison with realization of architecture of the project. For example now I develop the project in team of developers. It stored on VSS and not depend on local settings developers computer. But its not thought over project architecture would be a big problem.

Bad, bad, bad. Never copy anything to the lib/ext directory of the jre. Learn how to use and set classpaths properly. If he has the proper jarfile, and places it in the WEB-INF/lib directory of the web application he is trying to reach, the classes will be found. If they are not, he has done something wrong. Either copied it to the lib directory of the wrong application, not copied the right jarfile, or, maybe, somehow or another, changed the tomcat configuration so that it does not look there. Sorry to say it, but all of those things are "his problem", and copying anything to the lib/ext directory is not the answer, regardless of whether or not "it works".

1) i have copied mysql-connector-java-3.0.8-stable-bin this zipped file and also imn unzipped form in WEBINF/lib .which jar files u are talking about please mention complete name .and
2) tell me by doing this can i remove the error encountering in kawapro also as i have mentioned above .

If your application runs in tomcat, place any and all jarfiles that it needs into the WEB-INF/lib directory of the application. You should know what the jars are, since you are the one writing the application. If you can no longer remember the name of a specific jar, then download it again. If, after placing the jar(s) in WEB-INF/lib, you still get a ClassNotFoundException, then you have either copied it to the WEB-INF/lib directory of the wrong application, or you have done something to the Tomcat configuration which prevents it from loading libraries in that directory. What that might be, I cannot even begin to tell you. All of this, of course, assumes that you are accessing the application through Tomcat, and a browser, of course. If you are attempting to execute something from the command line, then you should use the -cp option to specify the classpath (specifying the full complete path to each individual jarfile needed) when you type java (if the class will even function from the command line, which Servlets and JSPs definately will not (and most, if not all Beans, also should not, except indirectly, i.e. accessed from a different class which is executed).

oh my dear ! i am working with tomacat apache 5.0.28 but in order to work with beans there is also need to make a java file and place it in classes in tomcat directory .thats why i am running the program on jdk 1.5 +kawapro5 in order to connect the mysql database with jsp.
and the the jsp code is given below that i am using with this java file or class file

<%@page import="java.sql.*,java.util.*" %>
<jsp:useBean id="connBean" scope="page" class="ConnBean" />



<html>


<head>
<script>
function validate(f){
if(f.id.value.length == 0){
alert('User Id can no be empty.');
f.id.focus();
return false;
}
else    if(f.pswd.value.length == 0){
alert('Password can no be empty.');
f.pswd.focus();
return false;
}
}
</script>
<title>Log In</title>
</head>


<body onLoad="document.login.id.focus()">
<center>
<%
String query="insert into dbname(id,password) values('2222','222')";
String url=request.getRemoteAddr();
try
{
int t=connBean.executeUp(query);
if(t>0)
out.print("query successfull");
else
out.print("query not successfull");
}
catch(Exception e)
{}


</body>


</html>

but how tomcat will run it properly untill its class file(.java file) works properly.

thats why i am running the program on jdk 1.5 +kawapro5 in order to connect the mysql database with jsp

God forbid you'r learn how to do things properly, right?

And your JSP is a disaster area. NEVER put Java code in a JSP. And ALWAYS close ALL database resources as soon as you're done with them.

oh my dear ! i am working with tomacat apache 5.0.28 but in order to work with beans there is also need to make a java file and place it in classes in tomcat directory .thats why i am running the program on jdk 1.5 +kawapro5 in order to connect the mysql database with jsp.

Servlets are also java files (and JSPs are too, indirectly, since they will be translated to one the first time accessed after being changed). There is no reason you cannot test the Bean inside of the application. I assume you know how it will be used, so simply perform those actions through the browser (and at first you can have the bean log all sorts of things as kind of a "ppor man's" debug). Either that or write a separate Class that does nothing but call all the methods of the Bean, and which contains a main method, and call that. Then, like I said, include all jars on the command line with the -cp option. It's not that hard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.