Trying to create a JDBC based app that connects to a database allowing a user to edit a website.
Each time I test the app from eclipse I get a connection timed out error and have no idea why.
Sorry if it's a n00by question I'm new to JDBC. Any help or suggestions will be greatly appreciated.
Below is my code:
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://nkuwebdata.db.8750359.hostedresource.com",
username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM tbl_adminLogin");
while(rs.next())
{
String s = rs.getString("name");
System.out.println(s);
break;
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Below is the Error Message that is printing out in my console:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100)
at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92)
at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61)
at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133)
** END NESTED EXCEPTION **
Last packet sent to the server was 2 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100)
at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92)
at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61)
at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133)
Connection timeout generally occurs when the database server is not running.
dbname is missing in your code i guess. Just check this link
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
Several things: database not locally via localhost:3306 but over the net must pass through the firewall.
A port might be in order, for MySQL the default is 3306, as in localhost:3306.
Both these things might mean no connection, and would explain your error.
Things should be closed; otherwise one might run out of connections and such.
This can best be done with the try-with-resources construct.
And then one should almost use PreparedStatements to escape backslashes and quotes, and prevent SQL injection.
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try (Connection con = DriverManager.getConnection(
"jdbc:mysql://nkuwebdata.db.8750359.hostedresource.com",
username,
password)) {
String sql = "SELECT * FROM tbl_adminLogin";
try (PreparedStatement stmt = con.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String s = rs.getString("name");
System.out.println(s);
}
}
}
}
}
Check your connections credentials then also check server you are connecting is up and running.
Related
I am trying to connect from Java program to impala using the below driver
com.cloudera.impala.jdbc4.Driver
but i am getting a sql exception, bellow stack-trace
java.sql.SQLException: [Simba][ImpalaJDBCDriver](500310) Invalid operation: java.net.ConnectException: Connection refused: connect;
at com.cloudera.impala.hivecommon.api.HiveServer2ClientFactory.createTransport(HiveServer2ClientFactory.java:224)
at com.cloudera.impala.hivecommon.api.HiveServer2ClientFactory.createClient(HiveServer2ClientFactory.java:52)
at com.cloudera.impala.hivecommon.core.HiveJDBCConnection.connect(HiveJDBCConnection.java:597)
at com.cloudera.impala.jdbc.common.BaseConnectionFactory.doConnect(BaseConnectionFactory.java:219)
at com.cloudera.impala.jdbc.common.AbstractDriver.connect(AbstractDriver.java:216)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.gcs.virdict.InsertImpala.generateValue(InsertImpala.java:86)
Caused by: com.cloudera.impala.support.exceptions.GeneralException: [Simba][ImpalaJDBCDriver](500310) Invalid operation: java.net.ConnectException: Connection refused: connect;
I am also attaching the code below for reference
private void connectionImpala() {
try {
Class.forName(IMPALA_DRIVER_NAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = null;
Statement stmt = null;
try {
con = DriverManager.getConnection( "jdbc:impala://IP:21050/default;user=abcde;password=abcde");
stmt = con.createStatement();
// ResultSet rs = stmt.executeQuery(sqlStatement)
} catch (SQLException e) {
e.printStackTrace();
}
This is my code:
public static void main(String[] args) {
System.out.println("Start");
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
Properties properties = new Properties();
properties.put("user", "xxx");
properties.put("password", "xxx");
Connection conn = DriverManager.getConnection("jdbc:db2://xxx.xxx.xxx.xxx:50000/xxxxxx", properties);
Statement stmt = conn.createStatement();
String query = "SELECT DESC_UFF FROM UFFICIO";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString("DESC_UFF"));
}
conn.close();
System.out.println("Stop");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
I use a jar file and it works fine on Windows XP and Windows 7, on a different PC, but not on Windows 10.
On Windows 10 I have this error:
Start
com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.14.146] Eccezione java.net.ConnectException: errore durante l'apertura del socket sul server /xxx.xxx.xxx.xxx sulla porta 50.000 con il messaggio Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.ed.a(ed.java:320)
at com.ibm.db2.jcc.am.ed.a(ed.java:338)
at com.ibm.db2.jcc.t4.wb.a(wb.java:434)
at com.ibm.db2.jcc.t4.wb.<init>(wb.java:93)
at com.ibm.db2.jcc.t4.a.b(a.java:354)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2030)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:732)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:680)
at com.ibm.db2.jcc.t4.b.<init>(b.java:334)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:475)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:116)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at main.TestDb2.main(TestDb2.java:20)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.ibm.db2.jcc.t4.w.run(w.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.wb.a(wb.java:420)
... 13 more
The version of DB2 database is 9.7 and Windows firewall is disabled.
Any suggestion?
What I am trying to do:
I want to connect my MS Access Database with Java 8. So I am using "UcanAccess" driver to connect to my Database.
What I am using:
Eclipse, Java 8, and MS Access database
My MS Access database path:
C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb"
I have the following Jar files in my project:
mysql-connector-java-5.1.35-bin.jar
ucanaccess-2.095.jar
jackcess-2.1.2.jar
hsqldb.jar
commons-lang3-3.4.jar
commons-logging-1.2.jar
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:106)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:2039)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1847)
at java.util.Collections.indexedBinarySearch(Unknown Source)
at java.util.Collections.binarySearch(Unknown Source)
at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2570)
at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:844)
at com.healthmarketscience.jackcess.impl.IndexData.access$3700(IndexData.java:47)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2335)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2273)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2256)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2218)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:376)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:282)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:153)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:2074)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1953)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:858)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:518)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:389)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:38)
at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:158)
at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:103)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ex01.main(ex01.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 28 more
code to connect to database:
public class ex01 {
public static void main(String[] args) {
String url = "jdbc:ucanaccess://C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb";
Connection con;
Statement stmt;
String query = "Select * from user";
try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
// Returns a ResultSet that contains the data produced by the query;
// never null
ResultSet rs = stmt.executeQuery(query);
System.out.println("User Data:");
System.out.println("FirstName\tLastName\tAge");
while (rs.next()) {
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
int age = rs.getInt("age");
System.out.println(fName + "\t" + lName + "\t" + age);
}
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
As you can see, jackcess 2.1.2 depends on commons-lang 2.6. The version you use (3.4) is not backward compatible.
Why do you have a JDBC driver for MySQL and for HSQLDB if your goal is to connect to MS Access?
I was trying to make connection with Oracle 11g via java and i have added ojdbc14 and ojdbc6 but still i am getting this error while compiling.Please help.
java.lang.ClassNotFoundException: com.oracle.jdbc.Driver
Goodbye!
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JDBC.main(JDBC.java:21)
And My Code is
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Open a connection
try{
Class.forName("com.oracle.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection
("jdbc:oracle:thin:#172.16.209.169:1521:heritage", "USERNAME", "PASSWORD");
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
You need to add JDBC driver to your class path.
java.lang.ClassNotFoundException: com.oracle.jdbc.Driver // this error shows that your application is missing oracle jdbc driver.
Download Oracle jdbc driver, then add it to your class path.
Your ojdbc$version.jar seems not to be in the classpath.
This question already has answers here:
connect to sql server using jdbc
(2 answers)
Closed 9 years ago.
Here I'm trying to connect oracle which is installed in local machine with DSN bam.
I'm getting java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception. Anybody please help me to fix this issue.
public class JdbcConnectionExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager
.getConnection("jdbc:oracle:thin:#localhost:1521:bam"
,"system","tiger");
Statement stmt = con.createStatement();
System.out.println("Created DB Connection....");
ResultSet rs = stmt.executeQuery("select * from tt");
while(rs.next()){
System.out.println(rs.getString("ename"));
System.out.println(rs.getInt("age"));
}
rs.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The Log is here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.bam.sqlex.JdbcConnectionExample.main(JdbcConnectionExample.java:14)
May be u have not added jar of oracle driver download jar from here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Undoubtedly, you are lack of Oralce jar, improt it into your project
Class.forName("oracle.jdbc.driver.OracleDriver");
You have to check if this class is added to the classpath.