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());
}
}
}
Related
I am trying to make a connection to a CA Datacom database from a Spring Java application. I am new to both Spring/Java and Datacom. After doing some searching I have come across a few resources (very few). In these I had and idea of what the connection should be. This is what I have in my application.yml so far for my datasource stuff, and I am not sure if it is right or not (doesn't seem to be):
spring:
application:
name: MyBatchApplication
datasource:
url: jdbc:datacom://hostname:hostport/ServerName=xxxx,ApplicationID=xxxx,UserID=xxxx,Password=xxxx
driver-class-name: ca.datacom.jdbc.DatacomJdbcDriver
I do have a jar file that is the driver for Datacom, and am importing that into my project with my build.gradle. I have code in my main project that tests the connection and it works that way. So that code is:
public static void main(String[] args) {
String url = "jdbc:datacom://hostname:hostport/ServerName=xxxx,ApplicationID=xxxx,UserID=xxxx,Password=xxxx";
try {
Connection conn = DriverManager.getConnection(url);
if (conn != null) {
System.out.println("We have a connection");
conn.close();
} else {
System.out.println("There is no connection");
}
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
SpringApplication.run(MyBatchApplication.class, args);
}
So the top part there works great. I get the We have a connection message. However, once the bottom part runs with the Spring application I get an error saying:
IO error sending or receiving native data: ca.datacom.db.DBIOException: MAINFRAME SERVER ERROR: REQUESTED SERVER NOT AVAILABLE
So I am at a loss. I know the server is available, and I know my credentials are correct since it works in the Java, but the Spring connection for some reason doesn't work.
I need to connect to my database from a standalone Java application.
I try with this code, but it gets stuck on the DriverManager.getConnection line.
try{
String url = "jdbc:mysql://192.168.2.11:1121/TEST";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Before");
Connection conn = DriverManager.getConnection(url,"test","test");
System.out.println("After");
} catch (SQLException e ) {
System.err.println("Sql exception! ");
System.err.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
The output I get is the following:
Before
and nothing else.
This is the db configuration page in MySQL(obviously it works via MySQL)
I'd appreciate any help
Same old question's but without answer
It seems that you are trying to connect to an Oracle database with MySQL driver. According to your configuration, you should change the connection string from jdbc:mysql://192.168.2.11:1121/TEST to jdbc:oracle:thin:#192.168.2.11:1121:1521:xe and use the Oracle driver oracle.jdbc.driver.OracleDriver instead of com.mysql.jdbc.Driver. You can download it here.
As suggested from Nigel Ren, the error was that is it an Oracle db.
I solved editing the code as follow
String url = "jdbc:oracle:thin:#192.168.2.11:1511:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url,"test","test");
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.
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
So I have a MySQL database set up on a Debian server and it works fine from a phpMyAdmin client. I'm currently working on a project to write a Java server that would be able to use the MySQL database that is already on this server through a JDBC connection. I've looked at many tutorials and documentations but all of them seem to just explain how to do client-side code, but I have yet to figure out how to even successfully open a JDBC connection to the server. As far as I am concerned, I believe that program has the drivers properly set up because it's not crashing anymore (I simply direct the Java Build Path of my program to the Connector/J provided by MySQL). As far as my program goes, this is what it looks like...
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
System.out.println("Started!");
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("Driver registered. Connecting...");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "password");
System.out.println("Connected!");
conn.close();
} catch (SQLException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
This is what's printed...
Started!
Driver registered. Connecting...
It's as if the DriverManager.getConnection(String) just freezes there. I'm sure this is a problem with the server because when I intentionally misspell localhost, or an IP address, the program crashes within 20 seconds. This just hangs there forever.
Sorry about this wall of text, but my final question is if anyone has any information what I should do or install on the server to get this to work? Thank you so much!
Try following:
public class MySqlDemo {
public static void main(String [] args) {
java.sql.Connection conn = null;
System.out.println("SQL Test");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = java.sql.DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?user=root&password=");
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}
System.out.println("Connection established");
}
You have to provide the name of the Schema to which you are connecting. Usually, the port is also added.
This is a sample connection string:
jdbc:mysql://repos.insttech.washington.edu:3306/johndoe?user=johndoe&password=jddb
3306 is the port and the first instance of johndoe is the name of the Schema. The second instance of johndoe is the username.
It could be that the Connector/J library is trying to use a named pipe to connect to the MySQL server rather than using TCP/IP and for some reason the named pipe isn't available. Try specifying a port number.
You may also want to try turning on some logging in Connector/J's configuration as described here.
Try putting port number and schema there
Try logging into database using some SQL client, may be SQL console
Try other drivers, may be some newer or perhaps older