Establishing JDBC through SID - java

I am connecting to Oracle 11g DB trough my java program.
I am using Service Name and not SID.
addr = jdbc:oracle:thin:#hostIP:1521:ServiceName
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(addr,un,pw);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(SELECT * from Table);
This works great. I am able to connect to DB and retrieve the data.
However, if I pass Service ID instead of Service Name, code doesn't work! I get exception.
I tried solution mentioned here - Java JDBC - How to connect to Oracle using Service Name instead of SID. But i still see the same exception.

addr should be in this way for service Name.
addr = jdbc:oracle:thin:#hostIP:1521/ServiceName
Using the Service Name
Syntax:
jdbc:oracle:thin:#//[host]:[tcpPort]/[service_name]
[host]: host name of the database server
[tcpPort]: database listener port
[service_name]: system identifier for the database
Example:
jdbc:oracle:thin:#//myhost:1525/myserviceDB
Please refer below article
https://docs.oracle.com/cd/E12518_01/central_office/pdf/141/html/co_ig/Output/appendix_url.htm

Related

Unable to connect remote Oracle DB in java stored procedure

I am trying to connect to a remote oracle database in java stored procedure in a traditional way. I am able to call the java stored procedure successfully. But I receive the exception "Missing In or Out parameter at Index 1" while making the connection. I am not sure how this error is related to making a connection in any way. But I don't have any other piece of code in java.
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#host:port:servicename","user","pwd");
}catch(Exception e){
//(Missing In or Out parameter at index 1)
//logged the error in the local database table
}
Can someone please help me where I need to make a change to connect to another oracle database.
Connection URL for servicename and sid differ.
Format:
With SERVICE NAME: jdbc:oracle:thin:#//host:port/ServiceName
With SID: jdbc:oracle:thin:#host:port:SID

Can't connect to Openshift mySQL

I'm trying to connect to my Openshift MYSQL database on a database called OTA
Whenever i try to connect i get the error -
java.sql.SQLException: Access denied for user 'admin881Rhs7'#'localhost' (using password: NO)
While i'm using a password
private String ConnectionString = "jdbc:mysql://localhost:3306/OTA?"
+ "user=admin881Rhs7&password=9JDlLn1r****";
public ResultSet ExecuteSQL(String query) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager.getConnection(ConnectionString);
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
resultSet = statement.executeQuery(query);
// writeResultSet(resultSet);
I can't seem to connect to the remote database using this connection, but I can logon onto the database using PHPMyAdmin
Note: I've tried changing "localhost" in my connection String to the IP of the server on database and doesn't work either
I've also granted my user previlages and still says (using password: NO )
Any help is appreciated , please note that this is on open-shift servers.
If you are running this app on OPENSHIFT you need to replace the localhost by MySQL server IP, you can get the IP using this command
rhc port-forward -a <mysql-gear-name>
and then used the same IP DriverManager
DriverManager.getConnection("jdbc:mysql://X.X.X.X:3306/OTA", "admin881Rhs7", "9JDlLn1r****")

How to connect to a database using a distributed API?

I'm having trouble with my distributed application in java netbeans the program is running perfectly on the original machine where i created it. but when i build it and make a distribute folder and follow the instructions and try to run it, I got an error that localhost on port 1527 has been refused.
here is my code on my do connect function
public void DoConnect()
{
String host = "jdbc:derby://localhost:1527/KempDB";
String uName = "main";
String uPass = "admin";
try
{
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "select cv.checkvouchernumber, c.checknumber, paytoorder, bankcode, dateissued, amount from checkvoucher cv, checks c where cv.checkvouchernumber = c.checkvouchernumber and cv.checknumber = c.checknumber";
rs = stmt.executeQuery(sql);.....
..........
}
catch(SQLException err){
.......
}
so this is the code I used to connect with the database server, the database server I used is built-in with java. Its a apache derby...
like I said in the original machine where I created the program runs ok without errors but when I distribute the program to another machine there's an error refusing the connection.
How can I connect to the local machine where my database is? maybe you can help me on this.
Are the database Servers running on these machines?
Are u starting the database server programmatically?
If you try to connect a database Server with:
jdbc:derby://localhost:1527/KempDB
This Server needs to be up and running.
For your case you should use an embedded Database.
For the case your Database is already an embedded Database, then you can try using this URL:
jdbc:derby:KempDB
instead of:
jdbc:derby://localhost:1527/KempDB
Hav a look on this one

Connection String to MySQL Temporary Table in Java

I am trying to create a temporary table in memory in order to store some data in it and use it within the application life time..
I am trying to do something as follows:
StringBuilder sb = new StringBuilder();
sb.append("CREATE TEMPORARY TABLE XmlItems ENGINE=MEMORY AS (");
// Continue with columns definitions
sb.append(")");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/");
Statement stat = conn.createStatement();
stat.executeUpdate(sb.toString());
But the following exception is being thrown:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I'm guessing it has to do something with the connection string..
Any ideas?
You haven't provided schema name in the jdbc connection URL. Try after adding schema name at the end of URL.
jdbc:mysql://localhost:3306/myschema
The connection string has to contain a database schema to connect to.
DriverManager.getConnection("jdbc:mysql://localhost:3306/myTestDB");
Also, make sure that MySQL is actually running. "Communications link failure" sounds more like the connection to port 3306 is refused.
Check to make sure that the database in this case mysql is started/running, the connection String look fine despite the fact the you've not added the database name to be used instead of "jdbc:mysql://localhost:3306/" use "jdbc:mysql://localhost:3306/databae_name"

Problems logging into MSSQL server from Java

I am trying to connect to MSSQL server 2008 on my localhost, but I am getting Errors
WARNING: ConnectionID:2 Prelogin error: host 127.0.0.1 port 1434 Error reading prelogin response: Connection reset
this error repeats like 20 times very quickly, then i get
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset
I suspect this is some wrong configuration of server. I can connect to external servers with no issues. I can also connect via management studio with no problems.
Please help me, I am getting sick of this issue :|
1) TCP protocol for MSSQL 2008 is disabled by default. You need to enable it from SQL Configuration Manager (in the same [Start], Programs folder as MSSQL):
http://msdn.microsoft.com/en-us/library/ms190425%28v=sql.105%29.aspx
2) To use TCP connections (even on localhost!) you need to allow MSSQL in Windows Firewall:
http://technet.microsoft.com/en-us/library/cc646023.aspx
3) You don't necessarily need to use TCP/IP in order to access MSSQL:
http://msdn.microsoft.com/en-us/library/ms187892%28v=sql.105%29.aspx
'Hope that helps!
To connect to MSSQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.
HOW TO CONNECT TO THE DATABASE A ‘Connection’ object represents a connection with a database. To establish the connection, use the method ‘DriverManager.getConnection’. This method takes a string containing a URL which represents the database we are trying to connect to. Below is the sample code for establishing a connection:
private String DATABASE_URL = "jdbc:odbc:embedded_sql_app"; // establish connection to database
Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );
Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.
QUERYING THE DATABASE The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the ‘Connection’ interface create instances of them. 1. Statement - created by the ‘Connection.createStatement’ methods. A ‘Statement’ object is used for sending SQL statements with no parameters.
2. PreparedStatement - created by the ‘Connection.prepareStatement methods’. A ‘PreparedStatement’ object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
3. CallableStatement - created by the ‘Connection.prepareCall’ methods. ‘CallableStatement’ objects are used to execute SQL stored procedures from Java database applications.
RETRIEVING THE RESULT A ‘ResultSet ‘is a Java object that contains the results of executing a SQL query. The data stored in a ‘ResultSet’ object is retrieved through a set of get methods that allows access to the various columns of the current row. The ‘ResultSet.next’ method is used to move to the next row of the ‘ResultSet’, making it the current row. The following code fragment executes a query that returns a collection of rows, with column ‘a’ as an ‘int’, column ‘b’ as a ‘String’, and column ‘c’ as a ‘float’:
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) { // retrieve and print the values for the current row
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f); }
This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html#Shahriar_N_Embedding_SQL_in_Java ) Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL. Hope you find this useful.

Categories