JDBC has issues creating users for mysql - java

So I'm trying to create a user in JDBC with
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "password");
Statement statement = connection.createStatement();
String command = "create user 'username'#'localhost' identified by 'pass'";
statement.execute(command);
Every time I try to run this I get the error below.
java.sql.SQLException: Operation CREATE USER failed for 'username'#'localhost'`
This I log into mysql as root and the same thing happens. I try the same statement and get Operation CREATE USER failed for 'username'#'localhost'. So I then run FLUSH PRIVILEGES and I can then create the user. As soon as I run the program again though the same thing happens.
What is the cause of this and how can I fix it?

It indicate that the user already exists or did exist. Thats why you are getting error while creating it

You can try the following Steps-
Drop the user DROP USER 'username'#'localhost'
Flush privileges - FLUSH PRIVILEGES
then Create the user - CREATE USER 'username'#'localhost'IDENTIFIED BY 'Identification' or CREATE USER 'username'#'localhost'IDENTIFIED BY PASSWORD 'password'

Related

Execute CommandCall/ProgramCall by JT400 with Disabled user profile

Our problem is as follows:
We have to connect to an ISeries with the JT400 and run some commands/Programs. The requirement is: Job is running in the QBatch with DISABLED user profile (because of security reasons).
We are able to make the JDBC connection, but getting the exception when executing the CommandCall/ProgramCall run() method. Our code is the following:
// This way we are able to create the JDBC connection without supplying a username/password
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
Connection connection = DriverManager.getConnection("jdbc:db2:*local;translate binary=true;prompt=false;naming=sql;libraries=MyLib");
// This way we are trying to connect to the Iseries and to execute CommandCall
AS400 as400 = new AS400();
CommandCall commandCall = new CommandCall(as400, "ADDLIBLE LIB(MyCmdLib)");
//Similarly executing ProgramCall as
ProgramCall programCall = new ProgramCall(as400, "/QSYS.LIB/OBJLIB.LIB/MYPGM.PGM", paramlist);
programCall.run();
and the exception is
com.ibm.as400.access.AS400SecurityException: User ID is
disabled.:NONACTUSR at
com.ibm.as400.access.AS400ImplRemote.returnSecurityException(AS400ImplRemote.java:2889)
at com.ibm.as400.access.CurrentUser.getUserInfo(CurrentUser.java:87)
at
com.ibm.as400.access.AS400ImplRemote.getPassword(AS400ImplRemote.java:1585)
at
com.ibm.as400.access.AS400ImplRemote.signon(AS400ImplRemote.java:3188)
at com.ibm.as400.access.AS400.sendSignonRequest(AS400.java:3465) at
com.ibm.as400.access.AS400.promptSignon(AS400.java:3043) at
com.ibm.as400.access.AS400.signon(AS400.java:4375) at
com.ibm.as400.access.CommandCall.chooseImpl(CommandCall.java:279) at
com.ibm.as400.access.CommandCall.run(CommandCall.java:713)
We searched on the internet but couldn´t find anyone with the same problem.
Thank you for reading our question.
You can't connect to the server with a disabled user.
Period. No exceptions.
It'd be a very insecure server if that was allowed.
You can create a User ID with password but set the Initial menu to *SIGNOFF for security reasons.
https://www.ibm.com/docs/en/i/7.2?topic=fields-initial-menu

Oracle Express 18c log on from Java jdbc application

new Oracle Express user here,
I did the db install w/o problems in my Win 64 environment,
used sqlplus from terminal to create user and grant him roles and privileges,
and finally this user works fine with the local db from Oracle SQLcl utility -
tables, queries etc.
The problem comes when I try to use the provided boilerplate code from Oracle for Java.
In Eclipse, in new project, with added jdbc8 jars the tested in SQLcl user creadentials
are rejected with exception: java.sql.SQLException: ORA-01045: user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Here is the Oracle boiler code used, with my local details :
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//localhost:1521/XEPDB1");
ods.setUser("c##dbUser");
ods.setPassword("dbUserPassowrd");
Connection conn = ods.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT 'Hello World!' FROM dual");
ResultSet rslt = stmt.executeQuery();
while (rslt.next()) {
System.out.println(rslt.getString(1));
}
Would appreciate your help, thanks!
Correct connection string had to be: "jdbc:oracle:thin:#//localhost:1521/XE",
to account for the DB name where user was created and granted priviliges.
As Oracle says:
user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Connect to the Oracle database as a privileged user (such as SYS) and
grant create session to c##dbuser;
(or whichever user name it really is).

Connect to MySQL database remotely in Java

I tried using this code to connect, but it doesn't work:
Class.forName("com.mysql.jdbc.Driver").newInstance();
//line 4 Connection con = DriverManager.getConnection("jdbc:mysql://example.com:3306/db", "user", "pass");
Statement st = con.createStatement();
String sql = ("SELECT * FROM users;");
ResultSet rs = st.executeQuery(sql);
if(rs.next()) {
int id = rs.getInt("id");
String str1 = rs.getString("imei");
System.out.print(id+"+++"+str1);
I receive this error:
java.sql.SQLException: Access denied for user 'user'#'ip' (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)
This user has all privileges to the database. How do I fix this?
Write following command into remote machine Mysql Server.
mysql> GRANT ALL ON *.* to root#'localhost' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
This command helps you to connect remote machine to your machine
I think you dont have Grant for 'ip'
Try this:-
grant all privileges on db.* to 'user'#'%' to allow connections originating from anywhere
Even though that the user already has all the privileges, you still need to deliver the user information in a line like this:
DriverManager.getConnection("jdbc:mysql://localhost/db-name","user","password");
You have to replace "db-name" for the name of your database, "user" for the user you created and "password" for the password of the user (if you don't have a password then leave it as "").
You can even create a variable type Connection and added to your code in case you need this in the future, like:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db-name","user","password");
Hope that helps!
To access database remotely from java you need to set database user access permission to access data from anywhere execute following query in your mysql terminal/
GRANT ALL PRIVILEGES ON database_name.* TO 'mysql_user'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
In above query % shows that mysql_user can access database_name database from any ip globally. If you want to allow specific ip then replace your ip with %

Connecting Java and Teradata: The UserId, Password or Account is invalid

I have been trying to connect to Teradata
Class.forName("com.teradata.jdbc.TeraDriver");
String connectionString = "jdbc:teradata://xxx.xxxxxx.com/database=xxxxxx, tmode=ANSI, charset=UTF8";
String user = "Rocket512";
String password = "aui8mn5";
Connection conn = DriverManager.getConnection(connectionString, user, password);
Got the following
Exception in thread "main" com.teradata.jdbc.jdbc_4.util.JDBCException: [Teradata Database]
[TeraJDBC 14.10.00.17] [Error 8017] [SQLState 28000] The UserId, Password or Account is invalid.
at com.teradata.jdbc.jdbc_4.util.ErrorFactory.makeDatabaseSQLException(ErrorFactory.java:300)
at com.teradata.jdbc.jdbc.GenericLogonController.run(GenericLogonController.java:666)
at com.teradata.jdbc.jdbc_4.TDSession.<init>(TDSession.java:216)
I know that the host is specified correctly since i did not get UnknownHost Exception.
Also I have double checked my userid and password are correct.
I ran query suggested by #beni23 (thank you)
select *
from dbc.logonoff
where logdate >= date '2013-10-31'
Here is the result that I got
What is Bad Password? I used SQL Assistant with this very password and it works great. Why cannot i connect with Java?
LDAP Authentication failures will not be captured in DBC.LogOnOff as a Bad Password event because the authentication doesn't take place on the database.
Typically the error message you are receiving, The UserId, Password or Account is invalid., is indicative of the user account being locked on the database.
SELECT U.UserName
, U.ProfileName
, U.DefaultAccount
, COALESCE(P.MAXLOGONATTEMPTS, S.MAXLOGONATTEMPTS) AS MaxLogonAttempts_
, U.LockedCount
, U.LockedDate
FROM dbc.UsersV U
LEFT JOIN
dbc.ProfileInfoV P
ON P.ProfileName = U.ProfileName
CROSS JOIN
dbc.SecurityDefaults S
WHERE UserName = 'Rocket512';
If LockedCount is not 0 than a failed logon attempt has occurred since the last successful logon to the database.
If LockedDate is not NULL it represents the date which the account was last locked.
MaxLogonAttempts_ will tell you how many times you can attempt to logon using database authentication (TD2) before the account is locked.
A couple of things I would suggest:
Remove whitespace between the parameters of connectString
Put the User and Password parameters in the connectString
Using original code above modify the connectString to add: ,ACCOUNT=$AMRWRW&DrT&r which should match what is returned by the query in my response above (I have added DefaultAccount).
EDIT: 11/12/13
May I suggest you download Teradata Studio Express and attempt to make a connection to the same Teradata system using JDBC much like you are here in your code. This may help shed light on the parameters you need to specify in your connection string in order to make the connection successful. You should be able to setup your connection parameters in Teradata Studio Express the same as you have here in your code and see if it works.
Using LDAP as the logon mechanism for a user that has not been granted the explicit right to logon with a NULL password has resulted in the error message `The UserId, Password or Account is invalid.'. I received this the other day using a privileged account without changing my logon mechanism from LDAP to TD2.
What does the following SQL return?
SELECT *
FROM DBC.LogonRulesV
WHERE UserName = 'Rocket512';
It may not return anything, which is okay. This simply means you ability to logon with that userid from any host on the system has not been explicitly granted or revoked.
EDIT: 05/22/18
The “Bad Password” event for an externally authenticated user may will appear in the event log when the provided password and the what is stored on the directory server do not match. In certain scenarios you can verify this by using ldapsearch from the PDN to submit an inquiry directly to the LDAP directory. You can find more details about using this command in the Security Administration manual. I’ve discovered this trying to triage a problem with a subset of user accounts that fail to authenticate to the directory. I felt it would be appropriate to update this answer with more details as my lead in statement at the top is not 100% accurate.
The following might not give you a solution, but might point you in the right direction. I think you'll want to check the dbc.logonoff table in teradata through a console to make sure that your user is not locked or get an idea whether your driver is hitting teradata.
select *
from dbc.logonoff
where logdate >= date '2013-10-31'
Reading this article Troubleshooting Security: The UserId, Password or Account is invalid. we can see the typical reason of this error.
Cause: LOGMECH=LDAP is specified, and the username/password credentials
are redundantly provided in the both LOGDATA and as separate
connection parameters.
Solution: Identify LDAP users via LOGDATA, or
via separate username/password parameters, but not both
simultaneously.
So, you should check this case. May be you can get connection without user/password
Perhaps you might have more luck with:
String driver = "com.teradata.jdbc.TeraDriver";
String conUrl="jdbc:teradata://xxx.xxxxxx.com/database=xxxxxx,USER=Rocket512,PASSWORD=aui8mn5,tmode=ANSI,charset=UTF8";
Class.forName(driver);
Connection dbConn = DriverManager.getConnection(conUrl);
If that doesn't work make sure to use the latest jdbc driver.
I had similar error and followed all the suggestions given in here. My account was not locked and I was able to connect to the DB with the same username and password, via SQL assistance editor.
The solution worked for me was adding following line in the connection string: LOGMECH=LDAP.
You need to know the logon mechanism for your Teradata, or you can reach out to your DBA team, as I did. So your connection string will look something like this:
String connurl="jdbc:teradata://xx/database=xx,USER=xx,PASSWORD=xx,tmode=ANSI,charset=UTF8,LOGMECH=LDAP";

code working with sa account but not with other

I have a jdbc properties file with
database.username=mfuser
database.password=mfuser
If I changed this to user/pass --> sa/pass then my web application works perfectly but with mfuser it does. I have create a user in SQL Server Security with name mfsuer and password mfuser but it still not working....
How I need to create the user mfuser so that it works? Any special privileges I need to provide for this user? I want it same like sa
Here is the error I am getting with mfuser
org.springframework.dao.PermissionDeniedDataAccessException: PreparedStatementCallback; SQL [select lm.login_id,lm.login_name,lm.login_password,lm.full_name, lm.email_address,lm.phone,lm.notes,lm.delete_status,lm.create_date,lm.create_by,lm.modify_date,lm.modify_by FROM Login_Master lm where lm.login_id=(select lm1.login_id from Login_master lm1 where lm1.login_name=? and lm1.login_password =?) ]; The SELECT permission was denied on the object 'Login_Master', database 'MCData', schema 'dbo'.; nested exception is java.sql.SQLException: The SELECT permission was denied on the object 'Login_Master', database 'MCData', schema 'dbo'.
The user mfuser doesn't have enough privileges to alter data in your tables, schema or database. You should access to your SQL Server 2008 using the sa user and grant enough privileges to the mfuser.
Follow the steps in this post to get your problem solved.
Looks like that the user mfuser doesn't have access on table your application needs to access.
If you created the database schema using sa user you have to give access to mfuser.

Categories

Resources