Connecting to remote postgres database using Java in Android Studio - java

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.

Related

How others can connect to my postgreSQL server so my login platform can work?

I am working on a hotel management system project and I want to make a login platform, which takes data from an AWS RDS PostgreSQL server that I have created. The problem is that people from other networks who I sent the .exe file of this project can't login but I can.
I have created the tables I wanted in pgAdmin4 and also I have installed the Postgres drivers in my project libraries.
Here is my connection class, where URL, user and pass are defined in the project:
public class ServerConnection {
static Connection getConnection() {
Connection connection = null;
try{
connection = DriverManager.getConnection(url, user, pass);
if(connection != null) {
System.out.println("Connected");
}
else {
System.out.println("Failed");
}
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
And below is the login method:
public void performLogin() {
PreparedStatement st;
ResultSet rs;
String user = username.getText();
String pass = String.valueOf(password.getPassword());
String query = "SELECT * FROM ADMINS WHERE username=? AND passw=?";
try {
st = serverConnection.getConnection().prepareStatement(query);
st.setString(1, user);
st.setString(2, pass);
rs = st.executeQuery();
if(rs.next()) {
hotelFrame2 hf2 = new hotelFrame2();
this.dispose();
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username / Password","LoginError",2);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
How can I fix it?
The problem is that people from other networks who I sent the .exe
file of this project can't login but I can.
It seems you are authorizing only your computer(IP) to connect to the RDS database, to solve your issue you have to open the flow to the other people by changing the security group.
By the way, using JDBC with AWS really hurt me, it is really an old way, it's better to look at JPA, Hibernate, or even some other AWS services.

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.

JDBC cannot login to LocalDB instance, but in SSMS I can log in

I am trying to connect to my database by JDBC on localhost. Connecting via windows authentication is no problem, but I want to connect via SQL authentication. Therefore, I created a login and a user corresponding to this login in my database. I can normally log in SSMS:
My connection string for JDBC:
jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123
Thrown exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'doszke'. ClientConnectionId:b7005fe3-904d-40c5-a89e-af0cb61250d6
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:258)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:104)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4772)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3581)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:81)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3541)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2395)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at main.Main.main(Main.java:38)
The username and password are the same, as those used for loging to SSMS.
Here my class code:
package main;
import java.sql.*;
public class Main {
private static ResultSet selectStan(Connection connection) throws SQLException {
String sql_stmt = "SELECT * FROM STAN;";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql_stmt);
System.out.println("Select executed");
return result;
}
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String userName = "doszke";
String password = "doszke123";
String url = "jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123";
try (Connection con = DriverManager.getConnection(url)) {
if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
As Mark Rotteveel pointed out, I was trying to connect to a LocalDB instance with JDBC, which seemed undoable. (ref: here)
However, I installed jTDS and added to my classpath, changed my connection string to
jdbc:jtds:sqlserver://./TestBazyDanych;instance=LOCALDB#EB7165FD;namedPipe=true
create a connection by the use of this connection string, username and password and it worked. The instance pipe number was taken from cmd line via
sqllocaldb i MSSQLLocalDB
There are few things need to check:
Did you create doszke user under the database and SSMS?
Are you able to login with doszke/doszke123 credentials in SSMS?
Please check 1433 port are open or not in your inbound and outbound firewall.
Trying to telnet on localhost 1433. If it's getting failed change below setting:
Go to Configuration tools -> SQL Server Configuration Manager Select SQL Server Network Configuration -> Select protocol in the right side window enable tcp/ip and restart the services in services.

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.

How do I connect a android app to a mysql database that is not local?

Hey I have found plenty of tutorials for people who are making the databases on their own computer but I am trying to connect to one that is not local
private class SignUpA extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
String url = "jbdc:mysql://mywbdb.cjymdxzuzy46.us-east-1.rds.amazonaws.com:3306/mysql";
String dbName = "profiles";
String driver = "com.mysql.jdbc.Driver";
Properties userInfo = new Properties();
userInfo.put("user", "user");
userInfo.put("password", "pass");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url + dbName,
userInfo);
CharSequence text = "Hello toast!";
Toast tost = Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_LONG);
tost.show();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I have imported the jdbc library and have tried using the DriverManager.registerDriver also but it still crashed.Also I changed the url and the username and password for obvious reasons. Please help, Thanks!
You wouldn't normally expose a mysql database to the Internet like that - your just asking to be hacked. Typically you would write a Web Service and expose that instead. Then your Android app would make calls to the Web Service to update the database.

Categories

Resources