I'm using mysql to create a connection with my database. The code is working fine, but what if i enter a wrong username/pass/host/database can i get a log message with the part is wrong? Here is the code:
private void connectDatabase()
{
try {
connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/"
+ database + "?autoReconnect=true", username, password);
System.out.println("[MySQL] The connection to MySQL is made!");
} catch (SQLException e) {
System.out.println("[MySQL] The connection to MySQL couldn't be made! reason: "
+ e.getMessage());
}
}
Ex: I enter a wrong password and i get this error log:
[MySQL] The connection to MySQL couldn't be made! reason: Wrong password.
Is this possible?
For MySQL, you will get an SQLExcpetion, something like below:
Caused by: java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113) ~[mysql-connector-java-5.1.18.jar:na]
...
SQLException will have SQLState = 28000, and vendorCode = 1045
Related
I have SQL developer installed and a properly configured DB, I create a user from sys like so:
CREATE USER random IDENTIFIED BY 12345;
GRANT ALL PRIVILEGES TO random;
I attempt to connect to the oracle SQL database with the ojdbc8.jar found in oracles website like this:
String url = "jdbc:oracle:thin:random/12345#localhost:1521:home";
try{
Connection dbConn = DriverManager.getConnection(url);
}catch(Exception e){
System.out.println("Exception: " + e.getLocalizedMessage());
}
However I receive this error:
Exception: ORA-01017: invalid username/password; logon denied
The last time I asked this question it was just populated by answers that have no actual answer to the problem at hand, I don't need to change the driver to a different one, I don't need to instantiate some sort of factory nonsense that just adds complexity, all I want to know is how I'm meant to connect to an account that is part of my DB so I can perform basic SQL functions.
Edit:
It just occurred to me that it is a pdb, is there a modification needed to the connection url that anyone can point out?
Try:
Connection dbConn = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:home", "random", "12345");
PDB connections need a slight tweak.
Please use localhost:port/sid instead of localhost:port:sid:
if (isPluggableDB) {
conn = DriverManager.getConnection("jdbc:oracle:thin:#" + hostName + ":" + hostPort + "/" + sid, userName, password);
} else {
conn = DriverManager.getConnection("jdbc:oracle:thin:#" + hostName + ":" + hostPort + ":" + sid, userName, password);
}
I had problem when connecting to Oracle Cloud Database from java code.
I have no problem connecting other non-cloud oracle databases.
I can connect to the Oracle Cloud Database with sql tools, except from the java codes.
The hostname, username and password is correct one, i don't reveal the real username and password.
Error: java.sql.SQLException:
SQLException: SQLState(null) vendor code(17002)
java.sql.SQLException: Io exception: Oracle Error ORA-12650: No common encryption or data integrity algorithm
My code as following:
String dbURL = "jdbc:oracle:thin:#192.133.133.23:1521:ORCL";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL, "username1", "password");
}catch(Exception)
{
e.printStacktrace();
}
/**
* The below one is for oracle thin client
Its worked for the ojdbc6 driver.
*/
public Connection conCheck() {
Connection con=null;
try { //step1 load the driver class
Class.forName("oracle.jdbc.OracleDriver");
Properties props = new Properties();
props.put("oracle.net.encryption_client", "REQUIRED");
props.put("oracle.net.encryption_types_client", "( " + AnoServices.ENCRYPTION_AES256 + "," +AnoServices.ENCRYPTION_AES192 + ")");
props.put("oracle.net.crypto_checksum_types_client", "( SHA1 )");
props.put("user", "username");
props.put("password","password");
//step2 create the connection object
con=DriverManager.getConnection( "jdbc:oracle:thin:#host:port:serveiceid",props);
System.out.println("Con"+con);
}catch(Exception e) {e.printStackTrace(); }
return con;
}
Seems like changing syntax in your connection string to lookup SERVICE_NAME instead of SID must help you connect your database.
String dbURL = "jdbc:oracle:thin:#192.133.133.23:1521/ORCL";
Additional Read : Thin-style Service Name Syntax
If this as well doesn't help, then would suggest to add below 2 lines to your sqlnet.ora in database.
SQLNET.CRYPTO_CHECKSUM_TYPES_CLIENT= (SHA1)
SQLNET.CRYPTO_CHECKSUM_CLIENT = requested
First mandatory check is to verify the oracle jdbc version. if you use incompatible version,we will get this kid of errors.
Based on the title, what must I configure in order to connect to Wamp Server using Java?
I'm having a trouble connecting to the Wamp server.
I have mysql-connector-java-5.1.9.jar on my C:\Program Files\Java\jre7\lib\ext. And also included on my libraries.
This is the database url jdbc:mysql://localhost:3306/testing?zeroDateTimeBehavior=convertToNull [root on Default schema]
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testing?zeroDateTimeBehavior=convertToNull", "root", " " );
PreparedStatement executeCommand = con.prepareStatement("INSERT INTO test VALUES(?)");
executeCommand.setString(1,name);
executeCommand.executeUpdate();
return "Success";
}
catch ( SQLException err ) {
return "Failed";
}
Above is my code to connect to my database to test whether it works or not. Everytime I run, it gives me the result Failed
Is it something to do with my code problem or my connection to database isn't established?
This is what I get when I run with stack trace. It has more but I didn't copy it
SEVERE: java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3996)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
remove space from password " " to "" and try again
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testing?zeroDateTimeBehavior=convertToNull", "root", " " );
I'm trying to write a java program to connect to the same MySQL database my website uses. I am using the same login details, minus the hostname ofcourse, but I am getting these errors:
SQLException: Access denied for user 'USER'#'HOSTNAME' (using password: YES)
SQLState: 28000
VendorError: 1045
I am using the hostname my host provided, and the password and user are the same details I have on my website.
Here is the code snippit:
public main() {
Connection con = null;
String mysql_hostname = "HOST";
String mysql_username = "USER";
String mysql_password = "PASS";
String mysql_database = "DB";
int mysql_port = 3306;
initComponents();
try {
con = DriverManager.getConnection("jdbc:mysql://"+ mysql_hostname +":"+ mysql_port +"/"+ mysql_database +"?user="+ mysql_username +"&password="+ mysql_password);
}
catch(SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
This is probably to do with some cPanel settings, but I am very new to Java, so asking couldn't hurt...right? :D
On cPanel as I remember when you create a db user there is also a hostname associated with that user default (localhost), If you are running your code on local machine you have to add your IP (public IP) or % in hostname for this db user from cPanel. So mysql will allow connection from your machine.
Note: adding % is risky, make sure you have a strong password.
SQL statement is not executedcom.mysql.jdbc.CommunicationsException: Communications link >failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException MESSAGE:
Connection refused
STACKTRACE:
java.net.ConnectException: Connection
refused at
java.net.PlainSocketImpl.socketConnect(Native
Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
at
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at
java.net.Socket.connect(Socket.java:546)
at
java.net.Socket.connect(Socket.java:495)
at
java.net.Socket.(Socket.java:392)
at
java.net.Socket.(Socket.java:235)
at
com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at
com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)
at
com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at
com.mysql.jdbc.Connection.(Connection.java:1555)
at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at
java.sql.DriverManager.getConnection(DriverManager.java:620)
at
java.sql.DriverManager.getConnection(DriverManager.java:200)
at
org.jtdemo.preparedst.main(preparedst.java:18)
** END NESTED EXCEPTION **
Last packet sent to the server was 1
ms ago. at
com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at
com.mysql.jdbc.Connection.(Connection.java:1555)
at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at
java.sql.DriverManager.getConnection(DriverManager.java:620)
at
java.sql.DriverManager.getConnection(DriverManager.java:200)
at
org.jtdemo.preparedst.main(preparedst.java:18)
My program is
package org.jtdemo;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class preparedst
{
//private static final String y = null;
public static void main(String arg[])throws Exception
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3036/mylib_db";
Connection con=DriverManager.getConnection(url,"nikki","dkm007");
String query = " select c.sub_category, b.title,b.author,b.b_key,ta.available_copies" +
"from Book_dim b,Category_list c,item_availablity_fact ta" +
" where sub_category = 'Mathematics' and " +
" c.category_id=b.category_id and " +
" b.b_key=ta.b_key " ;
/*ps = con.prepareStatement(" select c.sub_category, b.title,b.author,b.b_key,ta.available_copies" +
"from Book_dim b,Category_list c,item_availablity_fact ta" +
" where sub_category = ? and " +
" c.category_id=b.category_id and " +
" b.b_key=ta.b_key ");*///pass sql query ,no parameter passing
//ps.String(1, "Mathematics"); // set input parameter
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query) ;
while(rs.next())
{
String scat = rs.getString(1);
String ttl = rs.getString(2);
String auth = rs.getString(3);
int bkey = rs.getInt(4);
int avcop = rs.getInt(5);
//String b_key;
System.out.println("subcategory:"+scat+"title:"+ttl+"author:"+auth+"bookkey:"+bkey+"availcopies:"+avcop);
}
con.close();
//ps.close();
}
catch(Exception e)
{
System.out.println("SQL statement is not executed");
e.printStackTrace();
}
}
}
i am using jdk 1.6,mysql-connector-java-5.0.8-bin.jar,Eclipse Version: 3.5.2.
Please help.................
The exception tells you the problem. If your connection was refused, then there is something wrong with either the configuration of the client or the server. So the first thing to check is the JDBC URL. You need to verify the following:
The server is at localhost
The server is serving on port 3036 (the default port for MySQL is 3306, so this may be your problem)
You have a database on that server named 'mylib_db'
Your user name is correct
Your password is correct (in the future you may want to use placeholder text like 'password')
If all that information is correct, you need to examine your server to ensure that 'nikki' has permission to connect to the database over the network.
It looks like a network problem. It seems the database is not listening on the port you're telling java to connect to.
I would check that the port is indeed listening (via netstat), check any anti-virus, firewall, security programs, etc. If this all comes back good then perhaps a restart of your database program (myssql) would do the trick.