There seems to be an issue with Centos7 and the ojdbc6 driver that is used for Oracle connections. The connection hangs and or fails resulting in a timeout. The following is the simplest program I could derive to show this issue:
import java.sql.*;
public class Test{
public static void main(String[] args){
try{
Class.forName("oracle.jdbc.OracleDriver"):
}catch(ClassNotFoundException e){System.out.println(e);}
Connection conn = null;
try{
conn = DriverManager.getConnection(...);
}catch(SQLException e){System.out.println(e);}
}
}
The odd thing is this code executes perfectly fine on Centos6.
I don't think the JDBC drivers are operating system-dependent. However, for the issue you are facing, you can try setting the JVM property like this:
-Djava.security.egd=file:///dev/urandom
Please refer to this issue Oracle JDBC intermittent Connection Issue.
The issue on connection timeout and its relation with server randomness is mentioned in details there.
Related
I am trying to connect my sql server database to my code in java.
To get started I want to just make sure I can connect to the database via DSN but I am getting the error:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Here is my code:
package javaapplication1;
import java.sql.*;
public class JavaApplication1 {
public static void main(String[] args)
{
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SQLACCESS");
System.out.print("CONNECTION SUCCESSFUL");
}catch(Exception e)
{
System.err.println(e);
}
}
}
The DSN named "SQLACCESS" does not require a username or password. How should I go about connecting the two?
Everything starting with sun. is specific to Sun JDK. So it will not work on other instances.
The method you describe doesn't work in Java 8. They have removed the class. There is an article on how to workaround it (but I will look if there is something better): https://community.yellowfinbi.com/knowledge-base/article/moving-the-jdbc-odbc-bridge-from-java-7-to-java-8
You can find information on how to connect to MS Access databse here: http://www.javaxt.com/Tutorials/JDBC/How_to_Open_a_JDBC_Connection_to_Microsoft_Access
I know this has been asked a hundred times and I think I have read all the posts and tried every variation of the solutions. I'm using NetBeans and new to it. I'm sure I'm just missing some small step because it seems like its just not seeing the driver that I added to the library. This is the first time I have tried to connect to a database so please be gentle.
try
{
String host = "jdbc:sqlserver://Server:1433;Database";
String uName = "User";
String uPass = "Password";
Connection con = DriverManager.getConnection(host,uName,uPass);
System.out.println("Your are connected to SQLServer 2014");
}
catch (SQLException err)
{
System.out.println(err.getMessage());
}
You forgot to register the jdbc driver class.
Call
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
before calling Connection con = DriverManager.getConnection(host,uName,uPass);.
It will resolve the issue.
UPDATE
In documentation for new jdbc drivers it is declared that this step is not necessary. But in practical work, I have found that this step is required even for new drivers, otherwise you will get "No suitable driver found" error. This error occurs sometimes, for example it does not occur when you are making and running a console jar-application, but occurs when you have created and deployed a web-application.
So, I advise to register the jdbc driver class before getting the database connection via DriverManager.getConnection() call.
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 just keep getting Connection Failed, i dont know why, im running a server on UniServerZ and trying to get the SQL file from localhost.
Im using uniserverZ (Unicontroller.exe) and i made the .sqlite file using SQLite manager addon for firefox. Anyone can help me out here? Thanks!
Edit: Ok, now im just trying to load the sqlite file from my C drive, i have commented out the command that would load it from my localhost because it doesnt work either. Any help?
package ui;
import java.sql.*;
import javax.swing.*;
public class MySQLConnect {
Connection conn=null;
public static Connection ConnectDb(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\UniServerZ\\home\\Database\\db");
// Connection conn = DriverManager.getConnection("jdbc:mysql:\\localhost:3306\\Database\\db\\student.sql","root","root");
JOptionPane.showMessageDialog(null, "Connection Successful!");
return conn;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "Connection Failed");
return null;
}
}
}
This connection is strange
jdbc:mysql://localhost:3306//Database//student.sql
try
"jdbc:mysql://localhost:3306/Database",userName,password
where Database is the real data base name.
Note also single / vs //
You work with Sqlite database but used the jar of Mysql..
So correct this line.
String driver = "com.mysql.jdbc.Driver";
to
String driver = "org.sqlite.JDBC";
Change url and add the sqlite jdbc driver to succesfull connection with Sqlite DB.
And remove one / from below line after . jdbc:sqlite: need only / .
Connection conn = DriverManager.getConnection("jdbc:sqlite://localhost/Database/db/database.sqlite");
Thanks..
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