Can't connect to Openshift mySQL - java

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

Related

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

What will i write inside my Class.forName if i have jdbc:mysql://localhost:3306

im just really new to java and i have a hard time just to connect the database.
thank you for the help
i tried the derby but my database is in mysql using xampp.
Statement stmnt;
ResultSet rs;
try{
String host = "jdbc:derby://localhost:3306/population";
String uName= "root";
String uPass="root";
Connection con = DriverManager.getConnection(host, uName, uPass);
//execute some sql and load into the result set
stmnt = con.createStatement();
String sql = "SELECT * FROM users_admin";
rs = stmnt.executeQuery(sql);
//move the cursor the first record and get the data
rs.next();
int id = rs.getInt("admin_id");
String id_admin = Integer.toString(id);
String username=rs.getString("username");
String pwd = rs.getString("password");
ta_user.setText(username);
ta_pwd.setText(pwd);
ta_id.setText(id_admin);
}
catch( SQLException err ){
System.out.println( err.getMessage());
}
I've tried that, code in my other sample project and it shows an error:
java.net.ConnectException : Error connecting to server localhost on port 3306 with message Connection refused: connect.
Good news! You don't need a Class.forName and your JDBC driver loaded successfully. Now you need a mysql sever, or derby server you use two different jdbc url(s) in the question, listening on port 3306 to connect to.
You don't need it. Connection refused indicates that your driver is working perfectly, and that your MySQL or Derby database server (whichever of the contradictory URLs in your title and question is to be believed) isn't started, or possibly even installed, on the localhost.

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.

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

Categories