Changing tomcat 6 to tomcat 7 Giving error - java

My application running well in tomcat 6 with this connection variable
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Travel","root","root");
I have already tried many places to solve this but I couldn't find solution. But in tomcat 7 when I try to use connection variable it return Null pointer Exception.
Is there any way to change it in connection file?

Hads similar issue Tomcat7 with mysql-connector-java-5.1.26 that put in both my $CATALINA_HOME/lib and WEB-INF/lib, just in case. But it wouldn't find it until use either one of these two statements before getting the connection:
DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
OR
Class.forName("com.mysql.jdbc.Driver");
then followup with removing mysql-connector-java-5.1.26 from $CATALINA_HOME/lib and the connection still works.

Related

Netbeans JDBC Error "Unable to find suitable driver" after JDK update

I recently upgraded my jdk from 6 to 8 when I installed Eclispe for Android. Now, in NetBeans when I try to right click on the node for the database and select connect, I get the following error:
"Unable to connect. Cannot establish a connection to jdbc:derby://localhost 1527:/Animal using org.apache.derby.jdbc.ClientDriver (Unable to find a suitable driver)."
So far, I have tried uninstalling Eclipse and reinstalling Netbeans and that didn't work. I'm not getting any errors in my code aside from a popup that says the project is missing resources. Could someone please provide instructions on how to solve this issue? I've looked on the NetBeans' website and on this forum and there doesn't seem to be a clear procedure for resolving this.
If that's not a typo on your part your JDBC URL is malformed:
jdbc:derby://localhost 1527:/Animal
That's what I copied from your question. However, the correct URL would look like
jdbc:derby://localhost:1527/Animal
With the colon before the port, not after.
At Netbeans already states that this driver was tried but failed I'm quite sure it's got nothing to do with your driver setup. But if you want todouble check:
Here's a tutorial on how to add and enable oracle thin driver. I'd think it will work the same way for derby drivers.
I solved it in the following way:
The driver that you use to connect to the database may have deleted it or changed your address. So that:
You must put it back in the address that was indicated in the databases driver>
Drivers> (driver you use to connect)
Indicate the new address where you are
The error is in the path you are specifying in the driver or in the connection.
I had the same problem when trying to connect MySQL server to NetbeansIDE. Turns out the jdbc connector was an old version. You can read about the MySQL connector versions here. Either
download the latest version of MySQL connector or
add a new driver.
My problem was solved by adding a new driver.

MySQL Non Registering Driver

This is my first post so if I get anything wrong I apologise!
I am using Java in Eclipse to create tables in a MySQL Database. Everything has been working fine until it inexplicably stopped working the other day. My code runs but nothing happens to the database due to an error when I try to initialise the connection. The code actually runs to the end, the error is only viewable when I step through the code.
This is the constructor of my TableCreator class:
public TableCreator() throws SQLException {
host = "jdbc:mysql://localhost:3306/test";
dbName = "root";
dbPass = "pass";
conn = DriverManager.getConnection(host, dbName, dbPass);
query = conn.createStatement();
initialise();
System.out.println("Success.");
}
The error occurs on the line 'conn = DriverManager.getConnection(host, dbName, dbPass);'.
The stack looks like this just after I have attempted to execute this line:
TableCreator (1) [Java Application]
biz.cogitare.gpsperformancetool.TableCreator at localhost:53389
Thread [main] (Suspended)
Driver(NonRegisteringDriver).connect(String, Properties) line: 306
DriverManager.getConnection(String, Properties, Class<?>) line: not available
DriverManager.getConnection(String, String, String) line: not available
TableCreator.<init>() line: 23
TableCreator.main(String[]) line: 49
Daemon Thread [Abandoned connection cleanup thread] (Running)
C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe (9 Jul 2015 12:27:27)
I have spent many hours searching the internet for help on how to fix this but so far I have been unsuccessful.
Any help would be greatly appreciated.
Thanks!
Several things can cause such behaviour.
Check the driver is loaded.
If your JDBC driver is earlier than 4.0 you need to ensure the driver is loaded with
Class.forName("com.mysql.jdbc.Driver")
Or a similar string depending on the particular driver you are using.
Maybe you were doing another operation which loaded the driver before executing this code and that is why it did work. And maybe you are not doing that operation and that is why this code fails despite being the same code.
Check the driver file is in the classpath.
If you get ClassNotFoundException it means the JVM has not been able to locate your class. If you get LinkageError it means there is a version conflict. Get the most recent driver and reinstall it.
Check the driver is properly imported in eclipse.
Follow this instructions to import it : How to import a jar in Eclipse
If everything fails, reinstall.
Uninstall java, remove eclipse, re-install everything.
Insert the driver in the correct fashion and started a new project and copy your classes over.
If you are exporting the project as runnable jar,please make sure that you export mysql driver.jar
and as all above answers if you need to add
Class.forName("com.mysql.jdbc.Driver"); before conn = DriverManager.getConnection(host, dbName, dbPass);
If it not working, please share with us the stacktrace

Class [org.apache.derby.jdbc.ClientDriver] not found Exception [duplicate]

This question already has an answer here:
Class [org.apache.derby.jdbc.ClientDriver] not found. When trying to connect to db
(1 answer)
Closed 7 years ago.
![JAVADB_DRIVER_LABEL library missing][1]
I am trying to connect to a JavaDB through netbeans 8 IDE on Windows 7. i can connect it using the DB manager provided with netbeans in Services panel. But when trying to connect it through my code I get ClassNotFoundException for the org.apache.derby.jdbc.ClientDriver class.
I could not run the Apache tomcat server on my machine, so I am Using the Glassfish server 4 that comes with netbeans.
my code to connect to the DB is as follows:
String url = "jdbc:derby://localhost:1527/sample;UID=app;PWD=app";
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn = DriverManager.getConnection(url);
This similar code worked with SQL server, MS Access.
Please add your jdbc driver to the classpath of your project. It should work.
Currently, the class loader is unable to load your driver class as it does it does not find it in the classpath
Download the version of Apache Derby you need from here. Extract the zip and you should get derbyclient.jar which has the class org.apache.derby.jdbc.ClientDriver.
FYI : If your application runs on JDK 1.6 or higher, then you do not need to explicitly load the EmbeddedDriver. In that environment, the driver loads automatically.
You need to add derbyclient.jar to your classpath. You can find this jar in the download package of derby from here. This jar contains your org.apache.derby.jdbc.ClientDriver.
For more information, see this answer : where is org.apache.derby.jdbc.ClientDriver?

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. [duplicate]

This question already has answers here:
How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
(13 answers)
Closed 7 years ago.
I am using Java/Eclipse to connect to MySQL database but encountering the following error.
Error Message:
Unable to connect to databasejava.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Code:
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection myCon = DriverManager.getConnection("jdbc:mysql://localhost/myDB", "root", "password");
if(! myCon.isClosed())
out.println("Successfully connected to " + "MySQL server!");
myCon.close();
}catch(Exception ex){
out.println("Unable to connect to database" + ex);
}
%>
I know that this question has already been asked here but still I am unable to sort this thing out.
Environmental variables:
JAVA_HOME: C:\Program Files\Java\jdk1.7.0_51
CLASSPATH: .%JAVA_HOME%\lib;C:\Program Files\MySQL\Connector J 5.1.29;
How can I can solve this issue?
The Eclipse project doesn't know where you have the drivers for the database. You need to include them under the WEB-INF/lib directory so that they become part of the classpath.
Inside tomcat the external definitions do not apply.
Either deploy the driver jar with your application or add it to the extension library inside Tomcat. I would use the first if Tomcat is not expected to help you with e.g. connection pools or similar
if you are using eclipse then add MySQL jars in projects lib folder.
Yes it worked for me also for the error as
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.
Copied the sql connector jar file to lib folder of web-inf .
After copying this, the value from html to mysql database is sucessfully inserted
Thanks for the Help

java.sql.SQLException: No suitable driver found for jdbc:sqlserver [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 3 years ago.
I am working on a web application where I am creating MSSQLSERVER 2008
database dynamically.
But it's giving me
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=master
My code is:
String dbName = "db1";
try {
String url = "jdbc:sqlserver://localhost:1433;databaseName=master";
Connection connection = DriverManager.getConnection(
url,
"sa",
"roshan14121987");
Statement statement = connection.createStatement();
String sqlquery = "CREATE Database \"" + dbName + "\"; ";
statement.executeUpdate(sqlquery);
statement.close();
connection.close();
} catch(Exception e) {
e.printStackTrace();
}
I have added sqljdbc4.jar in lib. I have tried it on both NetBeans (with GlassFish and Tomcat Server) and Eclipse IDE(Tomcat Server).
On the other hand I have tried this with simple desktop application, it's working fine on both IDEs. With sqljdbc4.jar added in lib.
Before calling DriverManager.getConnection() you will have to load the SQLServer JDBC driver. You can do Class.forName("xxxx"); where xxxx is the appropriate driver class (fully qualified name with package prefix).
EDIT: Do this Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); to load the driver. Refer MSDN link for more.
Is sqljdbc4.jar in your war file under WEB-INF/lib. This is the alternate location to $CATALINA_HOME/lib and recommended to keep your web application self-contained. Further, if you should ever need to change servers, you need only drop the war into your webapps directory.
You might also need a Class.forName([database class]).newInstance, but that isn't necessary using a JDBC 4.0 driver. Good luck and if you have further problems, do leave a comment.
The error happen in the runtime on Tomcat Server,right? I think you need to make sure your sqljdbc4.jar deploy the in the Server.

Categories