JDBC connect with password but no username? - java

I am trying to connect to a server (via JDBC) which requires a password, but a blank username. When I try using an empty String ("") it causes an error:
java.sql.SQLException: invalid arguments in call
The code I am using worked to connect to another server (which requires username and password), so it is operable; just not in this specific case (empty string username). I also can access the connection for this specific server which requires no username via SQLDeveloper, so that's not offline or inaccessible. In SQLDeveloper I can enter an empty username field without incident.
From what I've read one can connect with JDBC with username and password, or with neither. But I can't find a solution for a blank username. There must be a hacky way around the problem.
private Connection getConnection() throws SQLException
{
OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:#" + serverName + ":" + portNumber + ":" + sid);
System.out.println("attempting connection");
connection = ds.getConnection(textFieldUsername.getText(), new String(passwordField.getPassword()));
System.out.println("connection established");
return connection;
}

Have you tried to connect through the DriverManager interface?
Connection conn = DriverManager.getConnection(url, props);
Where props doesn't have any entry for the username.

Related

Can't connect to the database?

I'm making a simple registration program to practise. For this I need to connect to a database. I tried diffrent things, but nothing worked out
I'm using Xampp and MySQL Workbench. I did't set any password or username. On MySQL Workbench it says:
Connection:
Name: demo
Host: 127.0.0.1
Port: 3306
Server: mariadb.org binary distribution
Version: 10.1.37-MariaDB
Connector: C++ 8.0.14
Login User: root
Current User: root#localhost
SSL: Disabled
import java.sql.*;
public class Driver {
public static void main(String[] args) {
try {
// 1. Get connection to the database
// Connection myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "student", "student");
//Connection myConnection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo");
String url = "jdbc:mysql://127.0.0.1:3306/mysql";
Connection myConnection = DriverManager.getConnection(url);
//2. Create a statement
Statement myStatement = myConnection.createStatement();
//3. execute sql query
ResultSet myResultSet = myStatement.executeQuery("SELECT * FROM students");
//4. process the result set
while(myResultSet.next()) {
System.out.println(myResultSet.getString("lname") + ", " + myResultSet.getString("fname"));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
java.sql.SQLSyntaxErrorException: Access denied for user ''#'localhost' to database 'mysql'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at jdbcdemo.Driver.main(Driver.java:14)
This answer is speculative, because I haven't actually tested it against MySQL, but here are two problems I see with your current approach:
You mentioned that you didn't create any username or password. However, as far as I know, even if you didn't create a username, then you would be using whatever default comes with MySQL (usually root).
Not setting a password is possible, but in that case you should still be using the three parameter variant of DriverManager.getConnection, which accepts a JDBC URL, a username (the default), and a password (empty, in your case).
Putting this all together, you might try:
String url = "jdbc:mysql://127.0.0.1:3306/mysql";
Connection myConnection = DriverManager.getConnection(url, "root", "");
Then, try to use your connection assuming the above works.
The connection URL is in this format:
mysql://<username>:<password>#<host>:<port>/<db_name>

Connect SYS DBA with no password using JAVA

I want to connect oracle DB with JAVA, I have code like this :
Class.forName("oracle.jdbc.driver.OracleDriver");
dbURL = "jdbc:oracle:thin:#localhost:1521:DB";
con = DriverManager.getConnection(dbURL, "sys as sysdba" , "");
return con;
it's not working, but I tried with command promt like this conn /as sysdba, it can be Connected. I have beed googling about this article but all of code not work.
Anyone can help.
Thanks
To log on as SYSDBA with the JDBC Thin driver you must configure the server to use the password file. So you need to provide password for sys.
Try catching the exception, it must provide more information about the problem.
public static Connection getConnection(String name, String pass) {
connection = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL, name, pass);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return connection;
}
Do you get the same error when trying to connect with a different username and password instead of "sys as sysdba" , "" ?
Also, in the dbURL, check if the url and database name are set correctly.
The problem most likely lies in the empty password. Try substituting "" with "()". If the sys really uses empty password, it must work. If it doesn't help, consider changing the password manually by alter user

Exception while connecting to the database

When I run this code:
String url = "jdbc:mysql://localhost:3306/staion_meteo";// phpmyadmin
String user = "root";
String passwd="" ;
Connection conn = DriverManager.getConnection(url, user, passwd);
System.out.println("you are connected to the data base : "+"station_meteo");
I get the following exception:
No suitable driver found for jdbc:mysql://localhost:3306/staion_meteo
Do this first ...
Class.forName("com.mysql.jdbc.Driver");
Use this
Class.forName("com.mysql.jdbc.Driver").newInstance();
before your connection. Also make sure mysql-connector.jar in your class path.

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException exception in CentOS server

Running a java code in CentOS server to connect mysql
My code is -
String server = "", url = "", password = "", user = "", databasename = "";
Connection con = null;
try {
server = "xxx.xxx.xxx.xxx";
databasename = "xx";
user = "user";
password = "pass";
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://" + server + "/" + databasename + "";
con = DriverManager.getConnection(url, user, password);
System.out.println(server + " Database connection established");
System.out.println(con);
} catch (Exception e) {
System.out.println("***************connection failed********************");
e.printStackTrace();
}
My database is running in same server.
I am running this code in my local system and other server for same database and its running fine but its not running on same server where my database available.
I am getting this exception -
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??????????????' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3361)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3295)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1852)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1975)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2470)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1669)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3336)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1979)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:287)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(libgcj.so.10)
at java.sql.DriverManager.getConnection(libgcj.so.10)
at testthread.ConnectionFactory.getConnection(ConnectionFactory.java:28)
at testthread.ConnectionFactory.main(ConnectionFactory.java:39)
is any one help me what is the problem.
This problem seems to be very similar to the following:
Stumped SQL Exception for JDBC
Try checking character set configuration/data in your local database server

Access mysql data base from another system using java

I am working on a interface in java swing.we have four system connected with a lan.the interface is for accessing the database from the other system in the same local area network i used the following code to access the database by giving the ip address,database name,tablename but i could not connect the other systems database.how can i do this?
public void dbconnection() {
String name = "";
String port = "3306";
String user = "systech";
String pass = "systech";
String dbname = "cascade_demo";
String host="192.168.1.61";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;
System.out.println("URL:" + url);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, user, pass);
String qry2 = "select * from item_master";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(qry2);
while (rs.next()) {
name = rs.getString(1);
System.out.println("Name:" + name);
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
Use below code
public void dbconnection() {
String name = "";
String port = "3306";
String user = "systech";
String pass = "systech";
String dbname = "cascade_demo";
String host="192.168.1.61";
try {
String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;
Class.forName("com.mysql.jdbc.Driver").newInstance ();
Connection con = DriverManager.getConnection(url, user, pass);
String qry2 = "select * from item_master";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(qry2);
while (rs.next()) {
System.out.println("Name:" + rs.getString(1));
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
Also, make sure to include jar file for connecting. You will get jar file here.
Update 1:
So, you have a
CommunicationsException: Communications link failure
I'm quoting from this answer which also contains a step-by-step MySQL+JDBC tutorial:
If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException:
Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
IP address or hostname in JDBC URL is wrong.
Hostname in JDBC URL is not recognized by local DNS server.
Port number is missing or wrong in JDBC URL.
DB server is down.
DB server doesn't accept TCP/IP connections.
DB server has run out of connections.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, follow the following advices:
Verify and test them with ping.
Refresh DNS or use IP address in JDBC URL instead.
Verify it based on my.cnf of MySQL DB.
Start the DB.
Verify if mysqld is started without the --skip-networking option.
Restart the DB and fix your code accordingly that it closes connections in finally.
Disable firewall and/or configure firewall/proxy to allow/forward the port.
Update 2
If your system is Windows, go to Start>>Run.
Type command. This will open command prompt.
Type "ping 192.168.1.61"
You might get reply in below format.
Pinging 192.168.1.61 [192.168.1.61] with 32 bytes of data:
Reply from 192.168.1.61: bytes=32 time=101ms TTL=124
If you don't get something in above format, then your MYSQL Server with ip 192.168.1.61 is NOT REACHABLE. Ask your team to start the server first. :(
If you have Linux version, open terminal and follow step 3.
Also check below link. Those might help you...
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
You should down load the jdbc driver and replace
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
by
Class.forName("com.mysql.jdbc.Driver");
If you still have issues asfter replacing Obdc stuff, please post the exception.
ALso check firewall settings and DB permissions.
If you'll get exception your app will not free system resources. This will work better:
} finally {
try {
rs.close();
st.close();
con.close();
} catch( Exception e ) {
e.printStackTrace();
}
}

Categories