i can connect mysql with java using eclipse in a java application with these statements
String unicode = "?useUnicode=yes&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ams-competation" + unicode,
username, password);
and it works good
but my problem is when i tried to connect to mysql with a server application i got this exception
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
what am i doing wrong?thank you all
Edit
i have added mysql-connector already
I think you have not included mysql drive jar in your web project.
put mysql-connector-java.. jar file in your web projects lib folder
for that you need to add the mysql-connector jar in the classpath.
This is because eclipse is not able to find the jar file you specified.
Hence put the jar file in the server`s lib directory
another way is to put jar file it in /WEB-INF/lib eclipse itself will notice it and get it.
Did you add the MySQL driver to the server?
It depends on the application server where the jar file is needed to be added:
Apache Tomcat: $CATALINA_HOME/common/lib
GlassFish server: GLASS_FISH_INSTALL_DIR\lib
Or add it the WEB-INF/lib folder of your web application
Try adding the mysql-connector-java-5.1.20-bin.jar file (downloadable from: http://dev.mysql.com/downloads/connector/j/ ), and restart the server.
Add the Connector/J jar file to the server's classpath (or lib folder)
add my sql connection jar file to your project. Its name will be something like "mysql-connector-java-5.1.13-bin.jar"
connect connector/J 8.0.11 with eclipse oxygen by following line of code.
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testing?autoReconnect=true&useSSL=false", "AF", "birru123");
Statement st = conn.createStatement();
Related
I have created a program that works perfectly in the IDE on NetBeans, but anytime I build the .jar file, the database connection stops working. I've already added the CLASSPATH to the MySQL Connector, as well as defined Class.forName("com.mysql.jdbc.Driver"); Still nothing..
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/gearheads";
Connection conn = DriverManager.getConnection(url,"root","HellOnEarth202021");
Statement stmt = conn.createStatement();
ResultSet rs;
String pid = txt_staffID.getText();
rs = stmt.executeQuery("SELECT name,timeraccess FROM staff WHERE staffid = '"+pid+"'");
while ( rs.next() ) {
String timeraccess = rs.getString("timeraccess");
String staffName = rs.getString("name");
getLogin = staffName;
System.out.println(staffName);
System.out.println("Users Admin Level: " + timeraccess);
if ( timeraccess.equals("1")) {
this.dispose();
new menu().setVisible(true);
System.out.println("Access Granted");
}else
System.out.println("Access Restricted.");
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
txt_staffID.setText(e.toString());
System.err.println(e.getMessage());
}
This, as it stands, outputs:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Where taking out the Class.forName outputs:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/gearheads
Please help me. This link is a screenshot of my libraries.
My Libraries:
If its runnning correctly inside Netbeans, then the MySQL driver is correct.
You have to search for Manifest file (MANIFEST.MF) and probably will have to unjar the contents of the driver inside your own JAR file.
I'll leave this answer by now, but will try to gather the details to bring it back here.
I also use NetBeans and when I generate the JAR file, its already copying the dependant libraries and generating the correct MANIFEST.MF.
I ended up with the following structure
MyProject\dist\lib\JdbcDriver.jar
and
MyProject\dist\MyProject.jar
Inside MyProject.jar, I have the following MANIFEST.MF:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_201-b09 (Oracle Corporation)
Class-Path: lib/JdbcDriver.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: myproject.MyMainClass
Please, verify with you are copying the lib folder altogether with your JAR file.
Just to leave here the final solution (#Nick Media final comment): you have to check "Copy Dependent Libraries" in Build>Packaging Project Properties.
It is not clear exactly what you are doing wrong, but you are clearly doing something incorrectly:
It is often better to use DriverManager.getConnection rather than Class.forName and a specific driver class name. This is the approach recommended by the Oracle Java Tutorial; see https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
If you are loading the driver via its classname, use com.mysql.jdbc.Driver with MySQL Connector/J 5.x and com.mysql.cj.jdbc.Driver with MySQL Connector/J 8.x.
If this is a webapp, make sure that the relevant driver JAR file is actually in your WAR file, and/or that it is being deployed correctly.
If this is a command line app, make sure that you include the driver JAR on the runtime classpath. (Especially if you are trying to run it outside of your IDE.) Alternatively, consider creating a shaded JAR that included all of the apps dependencies. (It is a bit hard to advise since you haven't told us if you are using a build tool like Ant, Maven, Gradle, etc.)
The driver doesn't actually need to be a compile time dependency, though there is little harm in doing that. (The harm is that you might accidentally add imports to the MySQL implementation classes to your app ... and run into problems.)
There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.
I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:
import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
Connection connection = null;
PreparedStatement query = null;
try {
out.println("Create the driver instance.<br>");
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Get the connection.<br>");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
query = connection.prepareStatement( "SELECT * FROM customers");
//...
} catch (Exception e)
{
out.println(e.toString()+"<br>");
}
}
//...
When I run the above code I get the following output:
Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:
Download mysql-connector.
Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
Opened the project properties in Eclipse.
Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.
Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?
As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.
An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.
See also:
How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
How should I connect to JDBC database / datasource in a servlet based application?
Where do I have to place the JDBC driver for Tomcat's connection pool?
JDBC CLASSPATH Not Working
Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application war or put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.
The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.
Rather than an empty main(), try something like this, adapted from the included documentation:
public class LoadDriver {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
}
}
On my platform, I'd do this:
$ ls mysql-connector-java-5.1.12-bin.jar
mysql-connector-java-5.1.12-bin.jar
$ javac LoadDriver.java
$ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver
On your platform, you need to use ; as the path separator, as discussed here and here.
Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.
What you should not do do (especially when working on a shared project)
Ok, after had the same issue and after reading some answers here and other places. it seems that putting external lib into WEB-INF/lib is not that good idea as it pollute webapp/JRE libs with server-specific libraries - for more information check this answer"
Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.
Good solution 1
Create vendor folder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to
add your folder to the build path
Project Properties -> Java build path
Libraries -> add external lib or any other solution to add your files/folder
add your build path to deployment Assembly (reference)
Project Properties -> Deployment Assembly
Add -> Java Build Path Entries
You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
Select the ones you want and hit Finish.
Good solution 2
Use maven (or any alternative) to manage project dependency
Just follow these steps:
1) Install eclipse
2) Import Apache to eclipse
3) Install mysql
4) Download mysqlconnector/J
5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/
then give the same path as the address while defining the driver in eclipse.
That's all very easy guys.
If the problem still persists,
Put the-
mysql-connector-java-5.0.8-bin jar in a place inside your Tomcat->lib->folder (No matter where you've installed your Tomcat). And change your environmental variable (Done by clicking Properties of Mycomputer -Advanced system settings- Environmental variables-And set a new variable name & variable values as the place where your lib file resides.Dont forget to enter a ; at the end of the path)
If still problem persists
Try downloading commons-collections-2.0.jar (http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) and paste the jar in the same place where your mysql jar resides (ie) inside Tomcat-lib.
Clean your project-Stop your server- Finally try to run.
Many times I have been facing this problem, I have experienced ClassNotFoundException.
if jar is not at physical location.
So make sure .jar file(mysql connector) in the physical location of WEB-INF lib folder. and
make sure restarting Tomcat by using shutdown command in cmd.
it should work.
The only solution worked for me is putting the .jar file under WEB-INF/lib . Hope this will help.
assuming your project is maven based, add it to your POM:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.
Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute
My issue was a little different. Instead of jdbc:oracle:thin:#server:port/service i had it as server:port/service.
Missing was jdbc:oracle:thin:# in url attribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's
java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'
for this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
you need to:
Import java.sql.*;
Import com.mysql.jdbc.Driver;
even if its not used till app running.
I'm trying to connect to a remote database using the following java code, but I get an error saying no suitable driver found.
DriverManager.getConnection("odbc:db2://url:port/dbname");
Class.forName("com.ibm.db2.jcc.DB2Driver");
What could I be doing wrong?
Two comments:
1. You have to load the driver first, before trying to use it
2. If I am not mistaken, the driver you are loading is a JDBC driver for DB2 UDB, not ODBC. The connection URL for it is "jdbc:db2//host:port/database"
So your code should look something like this:
Class.forName("com.ibm.db2.jcc.DB2Driver");
DriverManager.getConnection("jdbc:db2://host_or_ip_address:port/dbname");
I found the solution...I didn't add the JAR files for the driver.
You should add jar files of db2cc.jar,db2jcc_license_cu.jar and db2jcc_license_cisuz.jar files into your classpath location and these jar files held in your db2 installation directory location under java folder you got the above jar files
I am new to SOAPUI and have been trying to do query and procedure testing using SOAPUI.
I went through the SOAPUI documentation on net however I am not able to establish an connection to my DB..
I have copied the mysql jar in the lib folder of SOAPUI.
The exception I get is
Tue Nov 08 12:54:51 IST 2011:ERROR:com.eviware.soapui.support.SoapUIException: Failed to init connection for drvr [com.mysql.jdbc.Driver], connectionString [jdbc:mysql://DB SERVER IP:PORT/DB NAMES?user=username&password=password]
Can anyone please tell me what is wrong...
Here are the steps of what I did in SOAPUI for this JDBC thing:-
> 1.New project.
> 2. Clock on New Project.
> 3.Go to JDBC Connections tab.
> 4. Add a new Data Connection.
> 5. Specified unique name.
> 6. selected driver as mysql:com.jdbc.driver
> 7.I can only see to mention host,port and DB.
> 8.Mentioned Host,Port and DB..
> 9.Tested the Connection.
> 10.Got the above mentioned excpetion...
Please help anyone...
You could try the following. I had the same problem once and the following was working for me:
Download the JAR for MySQL again. In my case it happened that the JAR itself was corrupt.
Copy the JAR to %soapUI Folder%\bin\ext
This worked perfectly fine for me!
Download the mySQL connector tar.gz file from here http://dev.mysql.com/downloads/connector/j/5.0.html
Copy it at \SmartBear\SoapUI-Pro-5.0.0\bin\ext path
Extract it and Restart the application
Test the connection again! :)
Driver jar must be in ext folder and you also need to register a driver before actually using it.
for example to register postgresql driver use :
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("org.postgresql.Driver")
For me coping mysql jar in lib folder worked <%soapUI%/lib> and not <%soapUI%/bin/ext>
What worked for me:
Copy the ojdbc6.jar in lib and ext folders.
check your connections strings. It could be different.Are you trying to add using datasource? or script?
Remember to restart once you have downloaded the driver and included it in the directory.
I have written a code to check whether connection is successful or not.
But its giving error.
I have a oracle 10g express edition instsalled on my computer.
try{
String url="jdbc:oracle:thin:#//localhost:1521:XE";
String driver= "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
System.out.println(" Driver loaded ");
Connection con = DriverManager.getConnection(url,"system:,"manager");
System.out.println("Connection Successful");
} //catch block
The error given is:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Thanks for help.
you need to add oracle jdbc driver (jar) to your class path.
Looks like the JAR file containing the oracle.jdbc.driver.OracleDriver class is simply not on your classpath. Find it and fix that problem by adding the location (e.g., via the -cp option to java; the details of how to fix it will vary by the kind of application you're building).
Add ojdbc.jar in classpath.
Check this for How to Add JARs to Project Build Paths in Eclipse
You have to put the oracle_jdbc.jar file in the same folder of your code, or anywhere else and add that folder to your classpath.