Here's the code I'm using (Oracle database connectivity):
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:#loclahost:1521:XE","system","system");
System.out.println("connection is established");
Statement stmt=conn.createStatement();
int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
System.out.println("Save Sucessfully");
stmt.close();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
this.dispose();
Getting following Error:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could
not establish the connection
You've got a typo in your connection string - use localhost instead of loclahost
use localhost in DriverManager.getConnection
Here is the code :
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","system","system");
System.out.println("connection is established");
Statement stmt=conn.createStatement();
int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
System.out.println("Save Sucessfully");
stmt.close();
conn.close();
}
catch(Exception e) {
System.out.println(e);
}
this.dispose();
}
The error could occur if your JDBC driver is unable to connect to oracle. Check if your oracle service is running and no firewall is blocking your connection.
Check if oracle is listening in port 1521 or not, if not fix the port issue and then try connecting to your database.
Collectively the things to check ,
Check if the listener service is running
No firewall is blocking
Your service is listening on the correct port number that you
specified in your code.
Related
I am having a problem with the UCanAccess driver namely that when I attempt to connect to a database using the following code
public static void main(String[] args) {
//establish connection with database in order to execute sql queries
try {
conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\IT_PAT_Program_LOCALONLY\\IT_Pat_Database.accdb;showschema=true");
System.out.println("Connection Established");
} catch (SQLException ex) {
System.out.println("Could Not Connect to database\n"+ex);
}
//closes the database connection at program shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
conn.close();
System.out.println("Shutdown Succesful");
} catch (SQLException ex) {
System.out.println("An exception occured\n"+ex);
}
}
});
}
I am met with the following error:
Could Not Connect to database
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 invalid authorization specification - not found: Admin
The database is also connected as a persistence unit however since I'm unaware of any way to make use of that from within the code (Google has been no help) this method seems to be my only option.
We can not see where you are doing the authorization in your code.
user ?
password ?
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection(
"jdbc:ucanaccess://<mdb or accdb file path>",user, password);
This is my code for connecting to the Server remotely.
One thing more i would like to tell you guys that this code works only i am within my home network (the network in which server is installed) but i am unable to access it from other network
hey guys please fix it thanks in advance
public class SQLServerDemo
{
public static void main(String[] args)
{
// TODO code application logic here
try {
System.out.println("Hello from try");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://192.168.0.2:1433;user=demo;password=demo;databaseName=SQL_Demo";
Connection conn = DriverManager.getConnection(url);
if (conn != null)
{
System.out.println("Connected");
} else if (conn == null)
{
System.out.println("NotConnected");
}
String queryString = "select * from UserDetail";
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
}
catch (Exception e)
{
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
Exception
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host GAUTAM-VAIO, port 1433 has failed.
Error: "null. Verify the connection properties.
Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port.
Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
You cannot use 192.168.0.2:1433, it's valid only in your internal LAN network.
You have to find your external IP out of your LAN.
I did search here and found some useful code to connect to a MySql db using SSH tunneling. I essentially copied the following code from another example, of course replacing it with my own credentials, but I keep getting the following error:
"com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: "
Code:
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Properties info = new Properties();
info.put("proxy_host", "proxyhost.com");
info.put("proxy_port", "22");
info.put("proxy_user", "username");
info.put("proxy_password", "password");
info.put("user", "user_qa");
info.put("password", "password");
conn = DriverManager.getConnection("jdbc:mysql://qa2-commerce-master:4417/commerce", info);
Statement stmt = conn.createStatement();
} catch (SQLException er) {
er.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
This is all I have as I just want to get the connection to work. Any help would be greatly appreciated. I copied the code exactly from here:
Connect to MySQL using JDBC driver through a proxy
I am creating a simple example of jdbc .I am getting this error .I run my sql server on my mac machine .can you please tell me how to make connection with my sql and remove this error .I already include connector jar file .
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2489)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at HelloWorldExample.main(HelloWorldExample.java:17)
Caused by: java.net.UnknownHostException: local
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:877)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1230)
at java.net.InetAddress.getAllByName0(InetAddress.java:1181)
at java.net.InetAddress.getAllByName(InetAddress.java:1111)
at java.net.InetAddress.getAllByName(InetAddress.java:1047)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:248)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
... 15 more
Here is my code ..I am using mac
import java.sql.*;
/**
* HelloWorldExample : JDBC Tutorial that creates a simple database, stores in a single row
* and retrieves the data to then be displayed on standard out. The database is cleaned up
* to reduce clutter for this example.
* Author: Kevin Hooks
* Date: April 18th, 2012
**/
public class HelloWorldExample {
public static void main(String[] args) throws SQLException{
// Note that you can use TCP/IP by using this URL
// "jdbc:raima:rdm://localhost"
// The rdmsqlserver must be running to use TCP/IP
Connection Conn = DriverManager.getConnection("jdbc:mysql://local");
try {
Statement Stmt = Conn.createStatement();
try {
try { //Since the database is created here, it cannot be created twice
Stmt.execute("DROP DATABASE hello_db");
} catch (SQLException exception) {}
Stmt.execute("CREATE DATABASE hello_Db");
Stmt.execute("CREATE TABLE hello_table (f00 char(31))");
Conn.commit();
PreparedStatement PrepStmt = Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");
try { //Sets parameter value to a string
PrepStmt.setString(1, "Hello World!");
PrepStmt.execute();
Conn.commit();
ResultSet RS = Stmt.executeQuery("SELECT * FROM hello_table");
try {
while(RS.next() != false) {
System.out.println(RS.getString(1));
}
} finally {
RS.close();
}
} finally { //Cleans up by dropping database in this case
Stmt.execute("DROP DATABASE hello_db");
PrepStmt.close();
}
} finally {
Stmt.close();
}
} catch (SQLException exception) {
System.err.println("SQLException: " + exception.toString());
}finally {
Conn.close();
}
}
}
The hostname of the loopback address is localhost, not local.
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at HelloWorldExample.main(HelloWorldExample.java:17)
Line 17:
Connection Conn = DriverManager.getConnection("jdbc:mysql://local");
Your program can not establish a connection to the database, be sure that you have your connection URL correct.
local, should be replaced by localhost, or 127.0.0.1
You should also note, that you never actually select a database to use.
you have declared wrong connection string. please modified it according to below rule
Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name");
your URL should be localhost then add the database name
have a look at this
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] argv) {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
i'm trying to connect with jdbc to a specific schema in mysql server, the schema name is mining
when i'm trying to connect i get access to all the DB and therefore the executing statements apply to all the schemes in my db instead only to "mining"
this is how i establish a connection:
public class Mining {
Connection conn;
void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(
"jdbc:mysql://localhost/?currentSchema=mining","admin","admin" );
//I ALSO TRIED THIS: "jdbc:mysql://localhost/mining","admin","admin"
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
any thoughts?
thanks!
try,
private Connection connect = DriverManager
.getConnection("jdbc:mysql://localhost/mining?"
+ "user=admin&password=admin");
String databaseURL = jdbc:mysql://hostName:portNumber/schemaName?
Connection connectionObj = DriverManager.getConnection(databaseURL,userName,password);