Remote Oracle database connection using java(netbeans) - java

This is the code that I use to connect with the Oracle database that is on my pc:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:etecsa", "system", "asd");
It works but if I change localhost for an ip it can not connect to the database. I already deactivated the firewall but nothing.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co = DriverManager.getConnection("jdbc:oracle:thin:10.8.6.50:1521:etecsa", "system", "asd");
Where can I configure the oracle database to accept the connection from a specific ip and not only from localhost?

Check the documentation for DB URL
jdbc:oracle:driver_type:[username/password]#database_specifier
so in your case (if etecsa is SID) the url will be #host:port:SID
jdbc:oracle:thin:#10.8.6.50:1521:etecsa
if etecsa is a service name then use #//host:port/service_name
jdbc:oracle:thin:#//10.8.6.50:1521/etecsa

Related

Connecting a remote database that is behind a firewall via SSH

I am trying to write some Java code that connects to a remote database from my personal computer. I can currently ssh to these machines using a private/public key authorisation. I can access the GUI of these machines by creating SSH tunnels (with Putty).
I don't think the problems is with my code as I can create a DB using MySQL Workbench and can successfully query the db. When trying to access the db I use the same tunnell address that I use for the GUI. So my questions is down to how should I connect to the DB? Can anyone shed some light to my question? My code is posted below.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnector {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//Connection URL Syntax: "jdbc:mysql://ipaddress:portnumber/db_name"
String dbUrl = "jdbc:mysql://localhost:9092/db";
//Database Username
String username = "root";
//Database Password
String password = "root";
//Query to Execute
String query = "select * from employee;";
//Load mysql jdbc driver
Class.forName("com.mysql.jdbc.Driver");
//Create Connection to DB
Connection con = DriverManager.getConnection(dbUrl,username,password);
//Create Statement Object
Statement stmt = con.createStatement();
// Execute the SQL Query. Store results in ResultSet
ResultSet rs= stmt.executeQuery(query);
// While Loop to iterate through all data and print results
while (rs.next()){
String myName = rs.getString(1);
String myAge = rs.getString(2);
System. out.println(myName+" "+myAge);
}
// closing DB Connection
con.close();
}
}
The error I get is:
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)
This should probably be a comment, but my reputation doesnt allow for comments. I would venture to guess that the port on the server's network is blocking that port (whatever port you're using) but is allowing for ssh. What is this server/where/can you change the settings on any firewall to allow for connection?
If I get you right, the mysql is listening at port 9092 at the host you are connecting with ssh.
Then use this ssh command to connect:
ssh -L9092:localhost:9092 username#192.168.1.233
It creates a tcp listening socket at your local machine and tunnel any connections to port 9092 at local machine to the port 9092 at remote machine
Edit:
I missed that you are using putty. Then open connection settings dialog, then browse to ssh and find port forwarding. the notation is nearly the same: local port, to some ip:port at remote site: here you can find a step-by-step guide with screenshots

Access SQL Server 2008 R2 database from netbeans 8

I am completely new to java, and I want to make a connection to a remote SQL server 2008 R2 database (like 192.168.17.11) and load data from it.
Please suggest alternative ways if you know any.
First you must have JDBC driver for MS SQL, you have two jars (jtds-1.2.5.jar or sqljdbc4-2.0.jar), which added to your classpath
Second you need to create your connection as below:
String password="pass";
String driver= "net.sourceforge.jtds.jdbc.Driver"; // For sqljdbc4, use: com.microsoft.sqlserver.jdbc.SQLServerDriver
String username="user";
String URL="jdbc:jtds:sqlserver://serverIP:port/dbname"; // For sqljdbc4, use: jdbc:sqlserver://serverIP:port;databaseName=dbname
Class.forName(driver);
Connection conn = DriverManager.getConnection(URL, username, password);
// Use your connection here
// Don't forget to close the connection

Connecting to MySQL Server JDBC

I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

How do you find out the Oracle database's URL?

How can I find out the URL and port for an Oracle database?
Example:
"jdbc:oracle:thin:#host:port:dbName","userName", "password");
Is there an SQL command or log/configuration file I can look at?
With oracle, there is a tnsnames.ora file which defines database addresses. This file is normally found in $ORACLE_HOME/network/admin and is used by oracle clients like sqlplus or Toad. Here is a sample tns entry:
ORA11 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ORA11)
)
)
From this entry you can work out that your jdbc connection string would be:
jdbc:oracle:thin:#hostname:1521:ORA11
By reading the documentation which came along with the JDBC driver in question.
In case of the Oracle JDBC thin driver, you can find it here.
Specifying a Database URL, User Name, and Password
The following signature takes the URL, user name, and password as separate parameters:
getConnection(String URL, String user, String password);
Where the URL is of the form:
jdbc:oracle:<drivertype>:#<database>
The following example connects user scott with password tiger to a database with INSTANCE_NAME orcl through port 1521 of host myhost, using the Thin driver.
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:#myhost:1521:orcl", "scott", "tiger");
If you want to use the default connection for an OCI driver, specify either:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:scott/tiger#");
or:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:#", "scott", "tiger");
For all JDBC drivers, you can also specify the database with a Oracle Net keyword-value pair. The Oracle Net keyword-value pair substitutes for the TNSNAMES entry. The following example uses the same parameters as the preceding example, but in the keyword-value format:
Connection conn = DriverManager.getConnection
(jdbc:oracle:oci:#MyHostString","scott","tiger");
or:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:#(description=(address=(host= myhost)
(protocol=tcp)(port=1521))(connect_data=(INSTANCE_NAME=orcl)))",
"scott", "tiger");

Oracle connection manager Java Client example

I am new to Oracle connection manager. Can some help me with a Java Client code example to talk to a oracle database through Oracle connection manager.
Quote from oracle docs.
The Web server on which the Connection Manager is running is on host webHost and is listening on port 1610. The database to which you want to connect is running on host oraHost, listening on port 1521, and SID ORCL. You write the URL in TNS keyword-value format:
String myURL =
"jdbc:oracle:thin:#(description=(address_list=
(address=(protocol=tcp)(port=1610)(host=webHost))
(address=(protocol=tcp)(port=1521)(host=oraHost)))
(connect_data=(INSTANCE_NAME=orcl))
(source_route=yes))";
OracleDataSource ods = new OracleDataSource();
ods.setURL(myURL);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
The first element in the address_list entry represents the connection to the Connection Manager. The second element represents the database to which you want to connect. The order in which you list the addresses is important.
When your applet uses a URL such as the one above, it will behave exactly as if it were connected directly to the database on the host oraHost.

Categories

Resources