Connection to Postgres Database failes - java

I'm trying for the first time to JDBC to connect to a local database (Postgres) but I can't seem to actually made the connection. This is my code:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] argv) {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/training", "iam47662285",
"MY56KZDZ");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
And this is the error that appears when i try to execute it:
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
Nov 02, 2017 4:36:52 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:450)
at org.postgresql.Driver.connect(Driver.java:252)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:450)
at org.postgresql.Driver.connect(Driver.java:252)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)
I use Fedora, but i don't know if that's relevant. Anyone have any idea of why is this happening?

Your username/password is incorrect in the following code:
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/training", "iam47662285",
"MY56KZDZ");
Manually log in to psql and verify what the correct username/password is. If necessary ALTER USER "xxxx" WITH PASSWORD ''; to a new one that you'll remember.
Also hardcoding login details is a very bad idea.

Related

Unable to get Oracle database connection,oracle.net.ns.NetException: Undefined Error

I wanted to connect to an Oracle database using JDBC.
I installed ojdbc6.jar properly and wrote this code.
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:#192.168.127.129:1521:helowin";
String id = "msa_checkin";
String pass = "msa0526";
Connection con = DriverManager.getConnection(url, id, pass);
if(con != null)
System.out.println("Oracle success");
else
System.out.println("Oracle failed");
}
catch(Exception e) {
System.out.println("Error");
e.printStackTrace();
}
}
}
But the output looks like this.
Error
java.sql.SQLException: Undefined Error
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.kinth.DBConnect.main(DBConnect.java:14)
Caused by: oracle.net.ns.NetException: Undefined Error
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:385)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
... 7 more
I search for similar questions,but it doesn't solve my problem.
Unable to get Oracle database connection, should I format my computer?
No problem with Navicat.
Navicat Image
I tried using oracle6.jar and Oracle14.jar but neither worked.
Can you follow the instructions on this page and try to get a connection using the latest JDBC drivers?

java.sql.SQLException: Invalid Oracle URL specified Error

I am trying to connect Oracle Database with JDBC Driver. I am getting java.sql.SQLException: Invalid Oracle URL specified error. Please help me in resolving the issue.
package test_connection;
import java.sql.*;
public class TestConnection {
public static void main(String[] args) {
// TODO code application logic here
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver loaded successfully ");
Connection con = DriverManager.getConnection("jdbc:oracle:thin#:localhost:1521:XE", username, password);
System.out.println("Connection established successfully ");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Here is the stacktrace of the error.
run:
Driver loaded successfully
java.sql.SQLException: Invalid Oracle URL specified
BUILD SUCCESSFUL (total time: 0 seconds)
It is a typo, you misplaced the #:
"jdbc:oracle:thin#:localhost:1521:XE"
It should be:
"jdbc:oracle:thin:#localhost:1521:XE",
You can get some samples from github. Example., DataSourceSample

Create connection to SQL database using JDBC Driver- SQLException [duplicate]

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.

Java JDBC and Oracle Wallet Connection

How to use oracle wallet
Using this as reference. I am still unable to connect to Oracle Wallet. Sample code below.
Errors
--
java.sql.SQLException: encountered a problem with the Secret Store. Check the wallet location for the presence of an open wallet (cwallet.sso) and ensure that this wallet contains the correct credentials using the mkstore utility: java.io.IOException: oracle.security.crypto.cert.PKCS12.getAuthSafesAsList()Ljava/util/ArrayList;
at oracle.jdbc.driver.PhysicalConnection.getSecretStoreCredentials(PhysicalConnection.java:1314)
at oracle.jdbc.driver.PhysicalConnection.parseUrl(PhysicalConnection.java:1198)
at oracle.jdbc.driver.PhysicalConnection.readConnectionProperties(PhysicalConnection.java:982)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:646)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:428)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:38)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:691)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
--
Sample Code
public static void main (String [] args) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
determineAndSetTnsHome();
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:/#xxx");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
private static void determineAndSetTnsHome() {
String tnsAdmin = System.getenv("TNS_ADMIN");
if (tnsAdmin == null) {
String oracleHome = System.getenv("ORACLE_HOME");
if (oracleHome == null) {
return; //failed to find any useful env variables
}
tnsAdmin = oracleHome + File.separatorChar + "network" + File.separatorChar + "admin";
}
System.setProperty("oracle.net.tns_admin", tnsAdmin);
}
--
Notes
Confirmed that wallet is setup.
- Doing sqlplus $xxx connects fine.
- Doing mkstore -wrl -listCredential returns the entry after entering wallet password.
Thanks in advance.
Can you check out the blog for more details?
Check if you are using TLSv1.2 and Oracle Wallets require additional jars (oraclepki.jar, osdt_core.jar, and osdt_cert.jar)
Your wallet may be installed and configured correctly, but that does not mean the wallet is currently open. Anytime the database is restarted, the wallet needs to be opened.
Run a sample query against a known table in your schema in SQL*Plus (connecting is not enough)
If the wallet is not open you will get the following error:
ORA-28365: wallet is not open
This query should give you the status of the wallet:
select wrl_type wallet,status,wrl_parameter wallet_location from v$encryption_wallet;
In order to open a wallet, run the following command, replacing "myPassword" with the password you chose:
ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "myPassword";

Cannot connect to heroku PostgreSQL db

I will connect with a simple java programme to my heroku Postgres database. But if i try to connect i get an error. Can anyone explain me the problem or can solve this problem?
Thx :D
Code:
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
// ========> from heroku website
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename";
Properties props = new Properties();
props.setProperty("user", "someusername");
props.setProperty("password", "somepassword");
props.setProperty("ssl", "true");
connection = DriverManager.getConnection(url, props);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}...
Output:
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:138)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
...
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
...
Thanks Lukas Eklund!
My url should look like this:
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename
?user=someusername&password=somepassword
&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";
Add sslmode to the database url:
String url =
"jdbc:postgresql://ec2-107-20-214-225.compute-.amazonaws.com:5432/databasename?sslmode=require
I had the same problem but I had to use 2 settings to get it work:
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-.amazonaws.com:5432/databasename?ssl=true&sslmode=require
ssl=true
and
sslmode=require

Categories