My code like this
Connection con=DriverManigar.getConnection("jdbc:sqlite:D:\\data.db);
It work fine
But i encript my sqlite with password using
DB browser for sqlite
When i open it in any browser of sqlite it ask me the password then when i enter the pass it work fine
So how to enter the password of sqlit to connection in java
I do some thing like this
Connection con=DriverManigar.getConnection("jdbc:sqlite:D:\\data.db ;Password='pass' ");
Put it does not work with me
a variant of getConnection() (it's overloaded) is with url as first parameter, username as second, and password as third.
getConnection("url","root","1234");
Replace root with your username, and 1234 with your password accordingly
Related
Based on oracle document, I create a wallet
mkstore -wrl /tmp/wl -create
Add a credential
mkstore -wrl /tmp/wl -createCredential localhost:1521/myservice user pass
In my java application, I want to connect to the database via this wallet
public static void main(String... args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.setProperty("oracle.net.wallet_location", "/tmp/wl");
Connection connection = DriverManager.getConnection("WHAT TO PUT HERE?");
}
But I don't know how to fill the connection string.
I would like NOT to use tnsnames.ora
Thanks
In my experience, use of tnsnames.ora was required when using a wallet for authentication, even for JDBC Thin connections. The connection alias in tnsnames.ora is matched to the connection alias in the wallet to provide the correct credential for a given connection.
That said, the latest documentation seems to say that you can enter a connection string along the lines of myhost:1521/myservice or (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost-scan)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=myservice))) as the db_connect_string parameter in the wallet. This would presumably negate the need for tnsnames.ora, as long as your connection URL after the "#" matched the db_connect_string in the wallet.
You connection URL then looks something like this:
jdbc:oracle:thin:#myhost:1521/myservice
You can pass wallet related connection properties as part of the connection URL. You can skip using tnsnames.ora.
See JDBC developer's guide for some examples.
I'm trying to connect to oracle 11g through JDBC, when I'm passing the username and password in the getConnection() method I can connect to the database just fine. But ,when I'm trying to take the input from their user and pass those as arguments.
I'm facing this error
java.sql.SQLException: ORA-01017: invalid username/password; logon denied.
I'm using a thin driver and this is how my code looks like:
Connection c=null;
private void getConnection() throws SQLException
{
System.out.println("Enter login credentials");
Scanner login=new Scanner(System.in);
Object Username=login.nextLine();
Object Password=login.nextLine();
System.out.println("username"+Username);
System.out.println("password"+Password);
c=DriverManager.getConnection("jdbc:oracle:thin:#//localhost:(Omitted wantedly)/XE","Username","Password");
}
I'm able to connect to the database through sql command prompt with same credentials. I've also tried to unlock the user in which I'm in but it didn't help either. Please help !
just replace double quote from username and password.
c=DriverManager.getConnection("jdbc:oracle:thin:#//localhost:(Omitted
wantedly)/XE",Username,Password);
I would like to connect with my local MYSQL data base, which is installed along with XAMP server. I created a new User ie, " NewUser " and its password is "password". I given all PRIVILEGES to this user.
I write the code to connect with data base by using user "root" (No password for this user).
Its connected . Like bellow..
return DriverManager.getConnection("jdbc:MySql://localhost/database_name","root","");
Now I wrote the code to connect with same data base by another user ie, "NewUser" and its pasword "password"
return DriverManager.getConnection("jdbc:MySql://localhost/database_name","NewUser","password");
but its not connected.
The error in console is
java.sql.SQLException: Access denied for user 'NewUser'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor207.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:214)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:73)
at org.eclipse.datatools.enablement.internal.mysql.connection.JDBCMySQLConnectionFactory.createConnection(JDBCMySQLConnectionFactory.java:28)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
I give the host type while providing PRIVILEGES to this user, as " any host" ie."%".
If I change this to " localhost " or "127.0.0.1" its working.
So How can i use my database with " anyhost " PRIVILEGES to the particular user like "NewUser" .
If I got success here then I successes in connection to client live Data base..
Thanks to all and please let me out from this one.....
Since the first specified code works and also based on the reported trace, i'm pretty sure the problem is on the database, not the code syntax.
Based on the Mysql version, please try as an alternative to set privileges without specifying the any host (%) as based on Mysql documentation,
The simple form user_name is a synonym for user_name#'%'
Also flush privileges immediately after using FLUSH PRIVILEGES;
Just to make sure everything is correct, also run a
SHOW GRANTS FOR NewUser; and check if NewUser appears in the list with the corresponding permissions.
your URL is meant to be all lowercase -- `jdbc:MySql://localhost/database_name"
Is your database really called database_name?
Please try this syntax
return DriverManager.getConnection("jdbc:mysql://localhost/database_name?user=NewUser&password=your_password_here");
(just replace your password where it says "your_password_here")
EDIT - SECOND GUESS:
Its possible you have firewalled yourself by blocking 192.168.x.x range.
i assume you running windows. open up command prompt, type "ipconfig" press enter. see your ipv4 adress (should be something like 192.168.1.x)
Be sure your antivirus/firewall program permits connection from 192.168.1.x (which is yourself) and then try using that instead of "%" or "localhost"
If this doesn't work, close all your firewall/antivirus and try again.
Also try reloading privileges either by:
restarting xampp
FLUSH PRIVILEGES;
In order to give users access to your database, you need to specify a host where they should be allowed to connect from.
But please be careful: Even if you use a wildcard (%) as the host name, the user cannot connect from localhost. They can connect from ANY host, but not from localhost.
When connecting from localhost, I assume your mysql installation assumes you to be an anonymous user.
In order to allow a user to connect from localhost, you need to add a separate user whith "localhost" in the host field.
For more details, please refer to the MySQL documentation
Two of the accounts have a user name of monty and a password of
some_pass. Both accounts are superuser accounts with full privileges
to do anything. The 'monty'#'localhost' account can be used only when
connecting from the local host. The 'monty'#'%' account uses the '%'
wildcard for the host part, so it can be used to connect from any
host.
It is necessary to have both accounts for monty to be able to connect
from anywhere as monty. Without the localhost account, the
anonymous-user account for localhost that is created by
mysql_install_db would take precedence when monty connects from the
local host. As a result, monty would be treated as an anonymous user.
The reason for this is that the anonymous-user account has a more
specific Host column value than the 'monty'#'%' account and thus comes
earlier in the user table sort order. (user table sorting is discussed
in Section 6.2.4, “Access Control, Stage 1: Connection Verification”.)
GO to users/privileges/edit privileges/
change password or select no password
update the password in hibernate config file and restart the server.
Is it possible to connect to a MySQL database without specifying the username and password in Java code :
Connection con=DriverManager.getConnection("database url","username","password");
or is there any way to change the username and password using Java?
There is another method of the same name to connect anonymously (without username or password):
Connection con=DriverManager.getConnection("database url");
Source: https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html
I don't know if you can setup a default user, but you can most certainly set up a user with no password.
As for the second question, assuming your connection has adequet rights in MySQL, you can most certainly set the password on any user. The most basic way to do that is to do:
update user set password=password('new_pass') where user = 'someuser';
Optionally you may want to specify the host field as well in the where clause. This needs to be ran in the msql database.
I started getting
java.sql.SQLException: Access denied for user 'xx#xx.xx' (using password: YES)
from a servlet when trying to add new data to a database. Irrespective of the error, the data still gets written to the database. The database has been standing for 2 years + now and no updates have been made to the database, users etc. I was wondering if anyone came across this before.
I am trying to avoid a mysql reinstall at all costs .
The mysql log file /var/log/mysql.log appears to be empty. The tomcat5.5 logs have nothing but a few info lines.
I'm running Tomcat 5.5 with MySQL 5.0.51a-24+lenny1 (Debian)
Thanks!
I had experienced the same and it is resolved by doing trim() on username and password string variables.
conn = DriverManager.getConnection(db_url, user.trim(), pass.trim());
Just try to establish the connection like this:
public static Connection getConnection() throws SQLException {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/dbName"; // Change it to your database name
String username = "root";
String password = "yourPassword"; // Change it to your Password
System.setProperty(driver,"");
return DriverManager.getConnection(url,username,password);
}