This question already has answers here:
Where should the JDBC driver JAR files reside on a Tomcat deployment with a datasource?
(2 answers)
Closed 7 years ago.
So i followed all the steps listed in other posts to add mysql-connector-java-5.1.36-bin to my class path in intellij 12 and when i'm writing code intellij clearly sees it and lets me import it, but when I run the application inside of tomcat I get a ClassNotFoundException
Below are screen shots of my set ups and my code.
public void runLookUp(String s){
String url = "jdbc:mysql://localhost:3316/test";
String username = "java";
String password = "password";
System.out.println("Connecting database...");
System.out.println("Loading driver...");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}
try {
Connection connection = (Connection) DriverManager.getConnection(url, username, password);
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
I am aware that doing it in global libraries instead of my lib directory is bad form I did it originally there and moved it to global as an attempted fix to this issue and just haven't moved it back yet. Given that this seems to work for everyone else I assume its something in the way I have my tomcat deploying from intellij, any thoughts?
This has nothing at all to do with IntelliJ.
You need to put JDBC driver JARs in the Tomcat /lib folder, not in any project WEB-INF/lib.
Your code as written is still far from optimal:
Hard coded URL, driver class, and credentials make it difficult to change.
Username and password in plain text.
No JNDI lookup.
No connection pooling.
Related
I have a very interesting issue and I'm not having any luck finding the source of the issue.
My company recently updated to a newer version of Java and the old ODBC connection method for connecting to an MS Access DB is no longer working. So I'm in the process of updating to a new method.
I found Ucanaccess which seems to be a good alternative as the connection is read only.
So following the getting started I updated the details in the code for Ucanaccess.
This is where i ran into a interesting problem.
I've added the following to my project
ucanaccess-3.0.4.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.1.3.jar
and update the connection creation code to the following
System.out.println("Establisting Connection.....");
Connection con = DriverManager.getConnection("jdbc:ucanaccess://Z:\\Database\\test.accdb");
System.out.println("Connection Establisted.....");
The first database I used was password protected so I thought that might be causing the issue so I switched to a new database. The second database was on a shared drive so i mapped the location to the above. Even with those two changes I'm still getting the same issue.
The problem is that each time i run the DriverManager.getConnection line the code it never reaches the System.out.println("Connection Establisted....."); line. There is no error messages and the program is still running so no crash. The strange thing is that if i put an invalid path in i do get to that line. Though i do get an error saying the file doesn't exist.
I've had no luck tracking down a solution to this issue.
I think could be problems with the URL connection. It works for me:
public class ConnectionUcanaccess {
static
{
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("driver loaded");
} catch (ClassNotFoundException e) {
System.out.println("the class driver can't be loaded");
}
}
static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:ucanaccess:///Users/shared/Desktop/database.accdb");
}
public static void main(String[] args){
try {
Connection con = ConnectionUcanaccess.getConnection();
System.out.println("Connected: " + !con.isClosed());
} catch (SQLException e) {
System.out.println("Error:" + e.getMessage());
}
}
}
I'm currently working on a small project which involves writing information from a CSV file to a MySQL database.
I'm using netbeans and have added the MySql JDBC JAR file to the project which is needed for the connection. When running the code below the program does not move on past the driver manager.getConnection statement. I am lost for ideas at this stage. My username and password is definitely correct and I am certain I have the URL right. No errors are returned, the project seems to just get stuck.
I am using a database that is hosted by blacknight hosting services, would this make a difference?
public static void writeToDatabase()
{
try {
String url = "jdbc:mysql://172.16.2.10:57983/db1320939_sa63898_main";
Connection conn = DriverManager.getConnection(url,"u1320939_kd","svpgalway21");
conn.close();
System.out.println("It worked!");
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
Try catching Throwable instead of Exception. Maybe you're getting an Error while you connect to the DB and you wouldn't notice it. Also include a finally clause with a trace.
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "");
XAMPP is hosting MySQL on 3306
I linked the mysql connect jar
ebookshop is the name of a DB
http://i.imgur.com/9XJjLiX.png
I have a user root wit no password
But I am getting a
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ebookshop
...
I have run other JSP and it works, I have a print statement just before the conn statement and it prints, one after that does not.
Thank you
You need to add the MySQL JDBC driver to your app's classpath. The most straight forward way to do that if you're running in a servlet container such as Tomcat or Jetty, would be to place the driver jar file in your app's WEB-INF/lib folder.
Try this code :-
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e)
{
System.out.println("Error in Data base Driver:"+e);
}
try
{
String url="jdbc:mysql://localhost:3306/ebookshop", "root", "";
connection=DriverManager.getConnection(url,userName,pass);
}
catch (Exception e)
{
System.out.println("Error in database connection:"+e);
}
First register your driver. Then you can use the url.
Here's my code:
import java.sql.*;
public class DBConnector {
private static Connection conn;
public static void connectToDB()
{
//load the driver
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println ("Driver successfully loaded");
}
catch (ClassNotFoundException c)
{
System.out.println ("Unable to load database driver");
}
//connect to the database
try
{
String filename = "TopYouTubeVideos.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += filename.trim () + ";DriverID=22;READONLY=true}";
conn = DriverManager.getConnection (database, "", "");
System.out.println ("Connection database successfully established");
}
catch (Exception e)
{
System.out.println ("Unable to connect to the database");
}
}
The output is:
Driver successfully loaded
Unable to connect to the database
This has worked on a different computer than mine, connecting to the database through exactly the same code... does anyone have any idea why?
Thanks in advance :)
EDIT: I'm running Access 2007, Windows 7 64bit
By checking for the error, I get: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
From a little bit of research it seems this is a problem with my 'data source name'. I put my database file in the project folder, and the name is correct. Why is it not finding it?
EDIT: No, the database was the same on both computers. In the same folder as well.
EDIT: I think I may need to create a system dsm. Following the instructions on the internet dosent work though, because I dont have the same files as them..
EDIT: I've tried to install that but it hasn't made a difference. My version of access is 64 bit alongside my version of netbeans..
Try installing a database engine + JVM that are both running in the same archeticture (i.e. 32bit), otherwise, you would receive a mismatch exception. I assume your JVM is 64bit but your database engine is not, so try installing a 64bit engine from the following link:
http://www.microsoft.com/en-us/download/details.aspx?id=13255
I am working on a dynamic webapp on eclipse and is trying to access a remote MySQL database. I made sure that all the information is correct. However I can't seem to connect to it. Here's my getConnection method:
public static Connection getConnection() throws SQLException {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";
String user = "username";
String password = "password";
conn = DriverManager.getConnection(url, user, password);
System.out.println("CONNECTED");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(InstantiationException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return conn;
}
I am trying to see what the error is but because this is a web app, I can't see that system.out.println anywhere, so it's kind of hard for me to debug with this. Any suggestions on how to debug?
Is your MySQL server accessible from out of the server? I am asking because it is possible to disable external connection for MySQL server, more info is here.
Your credentials are OK (at least its format).
Otherwise, you create Class.forName("com.mysql.jdbc.Driver").newInstance(); but you don't assign it to conn variable.
In web applications, System.out prints to the server's logs file. If you are using tomcat, see logs directory inside tomcat's base directory.
If you are still struggling to see the System out in logs, try to test the class from standalone java class, you will clearly see what the problem is.