Can't connect to oracle db most of the times - java

Good evening everyone, I'm getting confused with some troubles I'm incurring opening a connection with my db.
The code I've listed down, works fine establishing the connection at work, but at home it's working funny. I mean, at first I used my ip instead of localhost and it was working fine. After restarting my computer, I got the " The Network Adapter could not establish the connection" error, and after 1 hour of efforts and simply putting localhost instead of my ip, it magically started working again, but guess what. Just after another restart, again with the same error.
I'm using eclipse neon the latest version, tomcat 9.0 on windows 7 64 bit and
I have bitdefender as antivirus.
Can someone help me? I don't understand what's happening.
public class Connessione {
private String driver="oracle.jdbc.OracleDriver";
private static Connection connessione;
private final String url="jdbc:oracle:thin:#localhost:1521:ORCL";
private final String userName="xxxxx";
private final String password="xxxxx";
public Connection getConnessione() {
return connessione;
}
public Connessione() {
try {
Class.forName(driver);
connessione= DriverManager.getConnection(url,userName,password);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean chiudiConnessione(){
try {
connessione.close();
return true;
} catch (SQLException e) {
return false;
}
}

Related

Connecting to remote postgres database using Java in Android Studio

This is my first time connecting to remote database using android studio.
Program logs : not-Connected.
I have used same function to connect in eclipse, but it doesn't work in AS.
static Connection connection = null;
public static void databaseConnect() {
String jdbcURL = "jdbc:postgresql://ec2-52********amazonaws.com:5432/d3****hg";
String username = "ve*****cgo";
String password = "efc**********f16";
try {
connection = DriverManager.getConnection(jdbcURL, username, password);
Log.d("log", "databaseConnect: Connected");
} catch (Exception e) {
Log.d("log", "databaseConnect: not-Connected");
e.printStackTrace();
}
}
I have tried adding mssql-jdbc.jar to libs folder and adding android.premission.INTERNET to manifest, but I am obviously missing some steps. I couldn't find solution online so I am posting this question so somebody can hopefully help.

How does my executable JavaFX file connect to a MySQL database?

I've recently finished working on my first JavaFX app.
It connects with a MySQL database that is set up on a local server.
Before using the application I need to start the servers running using Xampp.
Now I want to finally pack my app into an .exe file and use it.
I'm a complete newbie when it comes to the servers and databases. My question is - what do I do to make my app connect with the database itself once the user opens it?
Do I need to switch from a local host server to a remote server that will not require starting it each time?
My JavaFX app connects with MySQL using JDBC.
private static String url = "jdbc:mysql://localhost:3306/Finance?useSSL=false&serverTimezone=UTC";
private static String login = "root";
private static String password = "";
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, login, password);
return connection;
}
You can test your connection with a method like this:
public boolean canConnect() {
try {
con = DriverManager.getConnection(url, login, password);
//executed only if no errors are thrown
return true;
} catch (SQLException e) {
e.printStackTrace();
//can't connect
return false;
} finally {
//close connection if it was successful
try {
if (con!=null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
by calling it in your main method, or in your first stage like this:
if (!canConnect()) {
//notify the user
//start xampp or check connection to local server
} //else proceed
If you want to deploy your application with Xampp, you need to make Xampp to autostart when pc boots up, so the user don't have to start it manually in each boot.
If you are wondering how to auto-start your MySQL service in Xampp, you can find it here.

Connecting to Database contents to be used for a netbeans project

Im trying to gain access to my database using a project in order to input information like username and password so that if the input is correct, then i can access details about that certain account, but before all that, there's an error that says "java.lang.ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver"
and im not sure how to fix it, i checked other solutions saying to check my URL, which i have done and fixed but it still is an error
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
try{
//REGISTER THE DRIVERS
Class.forName(JDBC_Driver1);
Class.forName(JDBC_Driver2);
//ESTABLISH CONNECTION TO DB
System.out.println("Connecting to a selected Database....");
con=DriverManager.getConnection(URL,USER,PASS);
System.out.println("CONNECTED SUCCESSFULLY");
}
catch (Exception e){
System.err.println(e);
I am not sure which IDE you are using. I will explain this in terms of the Netbeans IDE.
Firstly, you will need to make sure you have a Glassfish type server. They can be found in the "services" tab.
Secondly, you need to make sure your derby database is connected. In netbeans if it is not connected, it will have a broken looking icon next to it. If this is the case, right click and hit connect.
One of the benefits to a derby database is that it has a built in set of drivers, so it is unnecessary to provide a driver class.
If you are completely starting out fresh and have not changed your username or password, the following code has all of the default information to connect to the sample database that is created when you download glassfish server.
public class DataAccessDerby {
private static final String URL = "jdbc:derby://localhost:1527/sample";
private static final String USER = "app";
private static final String PASS = "app";
Connection con;
Statement stmt;
public void openConnection() throws ClassNotFoundException, SQLException {
con = DriverManager.getConnection(URL, USER, PASS);
}
public void closeConnection() throws SQLException {
if (con != null) {
con.close();
}
}
public static void main(String[] args) {
DataAccessDerby derby = new DataAccessDerby();
try {
System.out.println("Connecting to a selected Database....");
derby.openConnection();
System.out.println("CONNECTED SUCCESSFULLY");
derby.closeConnection();
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e);
}
}
}
I hope this helps.

Debugging database connection leak in concurrent environment

I'm currently working on a project which was originally not build for high load.
My problem atm is that at some point during the stress test (30 users) the application seems to "get stuck" and when it releases it spits out a lot of exceptions. Unable to get managed connection for [MY_DS]
When I run only one user, it works like a charm so it has something to do with the concurrency.
I also checked if there were any unclosed DB connections at the end of one run and there were none, so at normal usage, there are no connection leaks.
My suspicion goes out to my open and close methods (because they are static). Here are the methods:
public static Connection getConnection() {
if (logger.isDebugEnabled()) logger.debugLog("getConnection()");
try {
return DSUtils.getDefaultDataSource().getConnection();
} catch (SQLException se) {
logger.errorLog("SQLException", se);
throw new ApplicationRuntimeException(MessageCodesConstants.ERROR_SQL_EXCEPTION, se);
}
}
public static void closeConnection(Connection con) {
if (logger.isDebugEnabled()) logger.debugLog("closeConnection");
try {
if (con != null) {
con.close();
}
} catch (SQLException se) {
logger.warnLog("SQLException while closing connection");
}
}
It is an EE application running on JBoss EAP 6.2.0 backed-up by a SQL Server 2008.
Can somebody point me in the right direction to find out where the solution can be found?

Android JDBC Connectivity problems

I am trying to connect to my website's MySQL database, and I have no knowledge of PHP so I decided to use JDBC. I followed some video tutorials (non JDBC) and I used their steps. I skipped the MAMP step though because I am not hosting the server off of my PC. It is being hosted locally because it is going to be a larger website.
So I have this code entered in my Login Activity (first screen you see):
Connection connection = null;
Statement statement = null;
String username = "username";
String password = "password";
String dbURL = "jdbc:mysql://216.26.176.52:3306/lifesizefoto";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(dbURL, username, password);
statement = connection.createStatement();
System.out.println("Connected.");
} catch (ClassNotFoundException error) {
System.out.println("Cannot connect");
} catch (SQLException error) {
System.out.println("Error: " + error.getMessage());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) { try {connection.close();} catch (SQLException ignore) {} }
if (statement != null) { try {statement.close();} catch (SQLException ignore) {} }
}
I have tried many variations to the .getConnection() statement, but I can't figure it out. I have also contacted the website host and he took down all firewalls for my IP and even opened up a special port for the app.
When I run my app, I get this error:
01-09 18:59:32.769: I/System.out(14178): Error: Communications link failure
01-09 18:59:32.769: I/System.out(14178): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I would appreciate any help. Thank you in advanced!
Two thoughts:
It's unlikely that your web site's MySQL server is bound to an external interface - it's likely only listening on the localhost interface. Your hosting provider should be able to confirm / possibly fix that for you.
Trying to connect a mobile app directly to a server database is probably not going to work well in the long run - I'd suggest that you either figure out how to write a server-side app (for your mobile app to connect to) in PHP, or find another language that your host supports, and do it in that.

Categories

Resources