What is the connection url of mysql database in OpenShift? - java

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);

Related

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

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?

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

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.

Make a connection to a MS SQL database in the local machine

I am trying to access a database which is located in my C drive in my local machine. I can access the data base through Microsoft SQL server Management Studio. However as the data base is not in a server my first question is "if even it's possible to access it through my java code."
-I have tried my local machine address and also the name of the server however non of them seems to be working.
String url = "jdbc:sqlserver://DAVE-PC\\SQLEXP/";
String dbName = "STORESQL";
// String driver = "com.mysql.jdbc.Driver";
String userName = "dave-PC\\dave";
String password="";
try {
// Class.forName(driver).newInstance();
Connection con =DriverManager.getConnection(url+dbName,userName,password);
java.sql.Statement stm= con.createStatement();
*DAVE-PC\SQLEXP/" is the name of my database. The username and password are windows authentication.
My second question is if I can't access the database like this, Is there any virtual server (compatible with microsoft SQL) that I can copy the database there and access it from the?
I think your mistake is in userName. for your PC you can try LOCALHOST or 127.0.0.1 Perhaps you would try this method to connect:
try{
server = "jdbc:sqlserver://127.0.0.1:1433;databaseName=STORESQL;selectMethod=cursor";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(server,"dave",password);
}catch(SQLException e){
System.out.println(e);
}catch(ClassNotFoundException e){
System.out.println(e);
}

Categories