java how to connect to a MySQL database in OpenShift - java

package library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class conexion {
private String server = "jdbc:mysql://" + System.getenv("OPENSHIFT_MYSQL_DB_HOST") + ":" + System.getenv("OPENSHIFT_MYSQL_DB_PORT") + "/" + System.getenv("OPENSHIFT_APP_NAME") + "";
private String user = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
private String pass = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
private Connection conn;
public Connection conectar() throws ClassNotFoundException, SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(server, user, pass);
} catch (Exception ex) {
Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex + ex.getMessage());
}
return this.conn;
}
}
I have a java application lodged in OpenShift but when I try to connect it to the cartridge mysql nothing happens when you connect. only it stays in the servlet and takes no other action

Depending on what type of java server you are running on OpenShift, try reading through one of these articles, which should help you get your database connection up and running:
Tomcat: https://developers.openshift.com/en/tomcat-ds.html
JBoss AS: https://developers.openshift.com/en/jbossas-ds.html
JBoss EAP: https://developers.openshift.com/en/jbosseap-ds.html
WildFly: https://developers.openshift.com/en/wildfly-ds.html

Related

No suitable driver found error even if the my-sql connector uploaded into the library

I am new to java and in a learning process. I'm working on connecting my JDE with MySql and I have followed every step necessary. but when I run the code, I got " No suitable driver found for jdbc.mysql://localhost:3306/dbname" error. I reviewed questions already in stackoverflow and other sources; but the provided solution didn’t work for me.
Any suggestions why i got this error even if I uploaded the mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar.a screenshot of my code and the error message
package JDBC;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
public class JDBC {
public static void main(String[] args) throws SQLException{
String url = "jdbc.mysql://localhost:3306/University";
String username = "root";
String password = "root";
String query = "select * from EngineeringStudents";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException e) {
//To do auto-generated catch block
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement statement = con.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
String UniversityData = "";
for(int i = 1; i <= 6; i++) {
UniversityData += result.getString(i) + ":";
}
System.out.println(UniversityData);
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
Bro use String url = "jdbc:mysql://localhost:3306/University". Use have missed a colon ':' after jdbc instead you're using '.'

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/db [duplicate]

This question already has answers here:
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]
(21 answers)
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
I have seen this error popping up many times. I have searched the web and tried stuff like adding the mysql-connector file to the buildpath, but nothing worked out for me.
This Code works when running with spigot, this is only another Linux user and not spigot.
Im running this Code in a plugin for the JTSServermod for TS3 on a Linux Debian with Java 8.
The full error message is:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at net.mysticsouls.TeamSpeakBot.utils.NameUUIDUtils.connect(NameUUIDUtils.java:31)
at net.mysticsouls.TeamSpeakBot.utils.Updater.start(Updater.java:11)
at net.mysticsouls.TeamSpeakBot.TeamSpeakBot.activate(TeamSpeakBot.java:45)
at de.stefan1200.jts3servermod.JTS3ServerMod.e(Unknown Source)
at de.stefan1200.jts3servermod.JTS3ServerMod.b(Unknown Source)
at de.stefan1200.jts3servermod.i.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
And my Code calling it is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class NameUUIDUtils {
private static final String host = "localhost";
private static final String port = "3307";
private static final String database = "db";
private static final String username = "username";
private static final String password = "*********";
private static Connection connection;
public static Connection getConnection() {
return connection;
}
public static boolean isConnected() {
return connection != null;
}
public static void connect() {
if (!isConnected()) {
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username,
password);
System.out.println("[NameUUID] MySQL connected!");
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("[NameUUID] MySQL failed to connect!");
}
}
if (isConnected()) {
try {
PreparedStatement preparedStatement = getConnection().prepareStatement(
"CREATE TABLE IF NOT EXISTS CoinSystem (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100), Strafpunkte INT(100))");
preparedStatement.executeUpdate();
preparedStatement.close();
System.out.println("[NameUUID] MySQL Table created!");
} catch (SQLException ex) {
System.out.println("[NameUUID] MySQL Table failed to create!");
}
}
}
}
What am i doing wrong.

Error 501 on Heroku

I have craeted a Web Application that can be deployed on Heroku by maven eclipse.
Group Id: org.glassfish.jersey.archetypes
Artifact Id: jersey-heroku-webapp
version: 2.17
then I followed this guide(1.5) to push it to Heroku.
https://jersey.java.net/documentation/latest/getting-started.html#heroku-webapp
I can not access my apple class from the http link- like this:
https://salty-refuge-2027.herokuapp.com/apple
and I am getting the error 500
I have tested it before without the JDBC procedure and I got the output Hello, from apple class therefore I guess it depends on the my implementation for the Heroku Postgres
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("apple")
public class Apple {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() {
try {
getConnection();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Hello, from apple class";
}
private static Connection getConnection() throws URISyntaxException, SQLException {
//I have modified my orginal ingredients.
URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv#
ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
+ dbUri.getPort() + dbUri.getPath();
Connection con = DriverManager.getConnection(dbUrl, username, password);
System.out.println("It works");
return con;
}
}
I appreciate any help.
You should call System.getenv("DATABASE_URL"). You are calling getenv with the value of the env var, which will return null.

Java - DriverManager.getConnection Access

i have the following code to test a link to a access database on a new server, everything works with the existing server and i am able to access the folder. !if.exists returns true and i can open the data base using Runtime.getRuntime().exec("run......... + f); so i know it can see the database. im unable to get a reason for the fail but its 100% failing at Connection conn = de.....
can anyone help me.
running gives following output - Java result: -1073741811
package testing;
import connections.LocalProperties;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass {
public static void main(String[] args) {
try {
File f = new File("\\\\***\\***\\****\\***.accdb");
if (!f.exists()) {
System.out.println("file does not exist" + f.getAbsolutePath());
return;
} else {
System.out.println("file does exist" + f.getAbsolutePath());
}
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getAbsolutePath();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String m_URLString = url;
String m_UserName = System.getProperty("user.name");
String m_Password = "*******";
Connection conn = DriverManager.getConnection(m_URLString, m_UserName, m_Password);
conn.close();
} catch (SQLException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
When running i get the following popups:
Java.exe
If you were in the middle of something, the information might be lost, for more information click here
Error signature
App name:java.exe appver 7.0.30.5 modname:msvcr80.dll
I think you are missing this line-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

JDBC connectivity with open office database

I am using openoffice database for my project....Please help me know how to connect to open office database using JDBC.
Below code is for JAVA. Transform to JSP accordingly.
import java.text.ParseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main
{
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws ParseException
{
try
{
String db_file_name_prefix = "c:\\mydbdir\\mydb";
Connection con = null;
// Load the HSQL Database Engine JDBC driver
// hsqldb.jar should be in the class path or made part of the current jar
Class.forName("org.hsqldb.jdbcDriver");
// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the state
// of the db.
// It can contain directory names relative to the
// current working directory
con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
"sa", // username
""); // password
Statement statement = con.createStatement();
//look at " for table name
ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");
//print the result set
while (rs.next())
{
System.out.print("ID: " + rs.getString("ID"));
System.out.print(" first name: " + rs.getString("firstname"));
System.out.println(" last name: " + rs.getString("lastname"));
}
statement.close();
con.close();
} catch (SQLException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
} catch (ClassNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Never tried it but seems like OpenOffice uses HSQLDB database underneath in file mode. Looks like you can connect to this HSQLDB database directly: Java: Creating a JDBC Connection to OpenOffice.Org Databases.
See also:
How to connect to a "normal" (not embedded) HSQL database?

Categories