My application uses a database based on sqlite-jdbc. When I generate the runnable jar file, connecting to my database works fine however if I let Proguard process my application, it breaks the database connection.
The following code establishes a connection with the database file's path submitted. When using Proguard, the message "Got connection!" is never printed hence getConnection() is stuck.
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
private void loadDatabase(String databaseName) throws SQLException
{
String databaseLibrary = "jdbc:sqlite:";
JOptionPane.showMessageDialog(null, "Getting connection...");
Connection databaseConnection = DriverManager.getConnection(databaseLibrary
+ databaseName);
JOptionPane.showMessageDialog(null, "Got connection!");
// ...
}
Only if the Shrink and Obfuscate options are disabled in Proguard the database connectivity does not break after processing.
Any ideas on how this can be fixed and why this happens?
Related
This question already has answers here:
JDBC connection to MSSQL server in windows authentication mode
(11 answers)
Closed 1 year ago.
I have made a simple java project in which I am attempting to connect to an SQL Server 2019 (Developer Edition) database. However, when I try to do so, I get a login authentication error.
This is my code for the project:
package javafxapplication12;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
*
* #author param
*/
public class FXMLDocumentController implements Initializable {
Connection con;
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
label.setText("Hello World!");
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Financials;user=dbo;password=;Trusted_Connection=False;MultipleActiveResultSets=True");
System.out.println("Connected to database !");
}
catch(Exception sqle) {
System.out.println("Sql Exception :"+sqle.getMessage());
label.setText("Failed");
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
However, when I compile this file, I am always getting this error:
Sql Exception :Login failed for user 'dbo'. ClientConnectionId:053ffe3f-aa4b-4c6b-86ee-df080cd91cf6
After reading for some time on Stack, I tried changing the hostname from localhost to myLaptopName, but I am still getting the same error, which leads me to believe that I am going wrong somewhere fundamentally.
Further, as suggested by some other users, I enabled SQL Server and Windows Authentication mode in Server Security settings, but even this didn't help resolve the error.
I am using JDK 1.8 with Netbeans 8.2 and mssql-jdbc-9.4.0.jre8.jar connector to connect to a MS SQL SERVER 2O19 database.
Also, I wanted to add that when I used this query SELECT HOST_NAME() in SSMS, I got the result myLaptopName. This is why I tried replacing localhost with myLaptopName.
Additional Information:
User name: dbo
Password: (no password)
myLaptopName refers to "LAPTOP-UQQOO5F7"
Database details:
SSMS Login Screen:
Update: I tried to change the database name in the link to something different, just to check if that is causing any errors. Inspite of purposefully entering a wrong DB name (eg. FinAANCNAials), I am getting the same error !
From the Screenshots, what I understood is that You are using Windows authentication to connect to the DB from SSMS, but you're using the SQL authentication connection string in the JDBC Code. There are 2 possible solutions
Change the exiting connection string to Windows authentication.
All You've to do is to remove the username and password fields and provide integrated security as True in the existing connection string. Like this
jdbc:sqlserver://localhost:1433;databaseName=Financials;integratedSecurity=true
Create a new SQL Authentication User and provide the credentials in the connection string.
You can create a new SQL user with the required roles in the system and replace the credentials in the existing connection string
Please refer to the following articles for more details
https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15
I noticed that my Play Framework application is not sending the read queries to the read-only MySql slave.
I am using
com.mysql.cj.jdbc.Driver as javax.persistence.jdbc.driver.
jdbc:mysql:replication://write-db-url,read-db-url/db_name as javax.persistence.jdbc.url
The db's are AWS aurora MySQL-compatible with multi-az replica.
I am using hibernate as ORM.
I am using play framework.
Am I missing any configuration/code?
Everything else looks good as stated in question, like jdbc driver and url.
Because in your question very less information is provided related to ORM or JPA and connection codes that you are using.
I'm here by providing a simple main program that you could use debug your issue. Once that is done, focus on your app to see, are you missing same thing.
Here is how JDBC driver determines, whether to connect master or read replica.
If connection mode is read+write, which is default, then it goes to master.
If connection mode is read, then it goes to one of read-replica.
Here officially documentation.
import java.sql.Connection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;
import java.sql.DriverManager;
public class ReplicationDemo {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
// We want this for failover on the slaves
props.put("autoReconnect", "true");
// We want to load balance between the slaves
props.put("roundRobinLoadBalance", "true");
props.put("user", "foo");
props.put("password", "password");
//
// Looks like a normal MySQL JDBC url, with a
// comma-separated list of hosts, the first
// being the 'master', the rest being any number
// of slaves that the driver will load balance against
//
Connection conn =
DriverManager.getConnection("jdbc:mysql:replication://master,slave1,slave2,slave3/test",
props);
//
// Perform read/write work on the master
// by setting the read-only flag to "false"
//
conn.setReadOnly(false);
conn.setAutoCommit(false);
conn.createStatement().executeUpdate("UPDATE some_table ....");
conn.commit();
//
// Now, do a query from a slave, the driver automatically picks one
// from the list
//
conn.setReadOnly(true);
ResultSet rs =
conn.createStatement().executeQuery("SELECT a,b FROM alt_table");
.......
}
}
Hope it helps.
I am using a Google Cloud SQL using Java-SQL connector. The issue I am facing is that the connection to database drops unexpectedly. While Googling I came across this question and tried the solution suggested in the same question.
In your console click the project, on the left side click Storage > CloudSQL then click on your database name. You will see an 'Edit' button on top. Click that and scroll down to Activation Policy, change it to Always On and then click save.
But I'm still facing the same issue. Fortunately I have been keeping the logs on Google App Engine and I have attached the snapshot of the exception that occurred while connecting to database.
Gist of the code that I've posted below is used to establish connection to the database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import com.google.appengine.api.utils.SystemProperty;
import static com.google.appengine.api.utils.SystemProperty.environment;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Development;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Production;
Connection con=null;
SystemProperty.Environment.Value env = environment.value();
if(env == Production)
{
System.out.println("Inside Production Phase");
// Load the class that provides the new "jdbc:google:mysql://" prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://<my-project-id>:<cloud-sql-instance>/<database-name>?user=<user-name>&password=<database-password>&useUnicode=true&characterEncoding=UTF-8";
}//if
else if(env == Development)
{
System.out.println("Inside Development Phase");
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://127.0.0.1:3306/<database-name>?user=root";
}//else if
con = DriverManager.getConnection(url);
Is anyone facing the same problem, Please help.
Got a temporary fix, used following parameters while making connection to Google Cloud SQL
url = "jdbc:google:mysql://my-app:mysql2/project-name?user=root&password=password&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
Reference URL: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
I have created a database in cpanel using MySQL® Database Wizard. I have created a java class to access the database. For remote access I've added my IP to Remote MySQL® allow section & I have also allowed all privileges to a specific username with a password. Keeping all that settings, from my home computer I still can not access the database. I am running this java application in NetBeans. As the errors say:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The source code goes like this:
package remoteserverconnection;
import java.sql.Connection;
import java.sql.DriverManager;
public class RemoteServerConnection {
public static void main(String[] args) {
Connection conn = null;
try
{
String url = "jdbc:mysql://domainIP:3306/DBNamne";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"UserName","password");
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Am I doing it in wrong way? Or is there any other way to connect that database from home pc?
It's possible that your host (judging from cPanel in the title) has a firewall rule set up that may be blocking access. That's very likely the case if it's a shared host which is the sort of service that typically uses cPanel.
I tried many ways to solve my issue, none of them worked, so here i am with the question. I created a local database with SQL Server Management Studio. It's name is CallCenter, i created a user account for it, granted every privileges and i can log into the DB with it, in the Managemenet Studio, everything works just fine here.
Now i use NetBeans, to create the connection. I downloaded the Microsoft JDBC driver, set up everything, the JDBC seems to be working fine. The problem is, it cannot connect to the Database. I set the log in options to both Windows & SQL. I tried to log in, with integrated sequrity ( windows account ) aswell as the created ( and working ) SQL user account.
None of them worked, i keep getting this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
'admin'. ClientConnectionId:1ce0b951-5ecb-49b4-a4d0-ff4a96af4ed2 at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)......
Here is the code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Try {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1434;databaseName=CallCenter;integratedSecurity=true;";
Connection conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.err.println("SQL Driver class does not exist!");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
I've been browsing the internet for hours, tried many solutions, but none of solved this problem for me. Please help me out here!
The Exception pretty much says it all - check your credentials (which you obviously have not given) with the database.
The Login failed for user 'admin' error which you get is a clear indication
that your login request reaches the SQL server but your credentials are wrong.
So either your username or your password is wrong, or maybe this login is not
configured for remote logins to the SQL server. Check your configuration on the server.
Use the other method of the DriverManager to get the connection using the Credentials.
When you have integratedSecurity=True, read this to get more idea on what it actually mean. It uses your windows credentials for login(With SQL Server).
So, make that false and provide credentials for the DB.
For your reference read this
[EDIT]
Try using this instead of integratedSecurity.
jdbc:sqlserver://HOSP_SQL1.company.com;user=name;password=abcdefg;database=Test