How to specify my databases name into JDBC -> MySQL connection? - java

What is the way to connect with JDBC to a mySql database that is online?
mysql.info.mi.us:3006 = corresponds to the server of the database
mydatabases_2 = my databases
my try returning this error:
java.lang.NullPointerException
Class.forName("com.mysql.jdbc.Driver")
connexion = DriverManager.getConnection("jdbc:mysql://mysql.info.mi.us:3306", "user", "password");

Just append it:
connexion = DriverManager.getConnection("jdbc:mysql://mysql.info.mi.us:3006/mydatabases_2", "user", "password");

Just a question: are you sure about the port?
The default port of mysql is 3306, while you're using 3006. Are you sure the port is right?

Related

Connect to an external Oracle DB using TNS

I need to connect to an external database to copy data from there to my table. I have a TNS file for this external database, and I am trying to connect using JDBC like this:
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);
...
But when trying to connect, i get the error java.net.UnknownHostException (host is not recognized). I guess the problem is that this is an internal host and I don't have access to it.
How to connect to the database using TNS?
You should not need the full TNS text. The following should suffice
getConnection("jdbc:oracle:thin:myuser/mypass#//"+host+":"+port+"/"+service);
If you have a tnsnames.ora then, you can provide the TNS alias as part of your connection string. Make sure you try to login to the Oracle Database through sqlplus using the connection string present in tnsnames.ora.
// dbname_tnsalias - It is the TNS alias present in tnsnames.ora.
// TNS_ADMIN --> Absolute path where tnsnames.ora is present.
final String DB_URL="jdbc:oracle:thin:#dbname_tnsalias?TNS_ADMIN=/Users/test/";

Establishing JDBC through SID

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

What is the connection url of mysql database in OpenShift?

I want to connect to the MySQL database in my java application. I have database name, user name and password. But what is the connection url and port number? What is the use of environmental variables?
Please try using below connection.
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));
$url = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$handler = new PDO($url, DB_USER, DB_PASS);

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****")

Connecting to remote MS sqlserver in java using windows authentication

I am using sqlserver with Windows authentication, with a Windows server.
I am trying to connect to a remote MS SQLSERVER on my local network using java eclipse but keep getting this error:
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The port number 64038 databaseName = Data is not valid.
Here is the code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("# - driver loaded");
String server = "moddbs169d.network.local\\Moddbs169d\\SQL2008";
int port = 64038;
String database = "Data";
String jdbcUrl = "jdbc:sqlserver://"+server+":"+port+" databaseName = "+database+";integratedSecurity=true";
Connection con = DriverManager.getConnection(jdbcUrl);
System.out.println("# - Connection obtained");
If all is successful it should tell me connection obtained. The local connection and name of the database are both correct, so that is not the issue. The jdbc driver is also installed and working properly.
You are missing a semicolon before the "databaseName=xxx" property.
Without the semicolon, you are setting the port number to "64038 databaseName = Data". Admittedly, the error message could have used brackets to make it a bit clearer.
See (http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx) for the form of the connection URL.

Categories