Exception while connecting to the database - java

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.

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>

JDBC connect with password but no username?

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.

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

How to connect to online mysql database from Java desktop app

I pave paid hosting, and i added my database to it. I am trying to connect to this online database from my java desktop application but, i got exception: Communications link failure.
here is my code:
public Kviz_DAO(){
try{
Class.forName("com.mysql.jdbc.Driver");
konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename?"+
"user=mydbuser&password=mydbpassword");
}
catch(Exception ex){
ex.printStackTrace();
}
}
Can anyone tell me what i'm doing wrong.
Check that, if permission is in the DB server for your IP. If not then GRANT the permission for the IP
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
if not working, check the FIREWALL.
The default port for MySQL is 3306. Are you certain that the hostname and port you're using is correct?
Try this:
konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename", mydbuser, mydbpassword);
Check this:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure
Can you connect using the MySQL admin?
sample code:
String url1 = "jdbc:mysql://localhost:3306/";
String db1 = "userdb";
String driver1 = "com.mysql.jdbc.Driver";
String user1 = "root";
String pass1 = "sarakrish";
try {
Class.forName(driver1).newInstance();
con = DriverManager.getConnection(url1 + db1, user1, pass1);
you should try this method:
String url = "jdbc:mysql://penal.ba:2082/";
String db = "mydatabasename";
String driver = "com.mysql.jdbc.Driver";
String user = "mydbuser";
String pass = "mydbpassword";
Class.forName("com.mysql.jdbc.Driver");
konekcija = DriverManager.getConnection(url+db,user,pass);
Download the connector J . Add it to your classpath and in the code:
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://remoteUri/database-name?"
+ "user=user&password=userpw");
Connection konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename?" + "user=mydbuser&password=mydbpassword");
//konekcija should be an object of Connection class.....I think :-p
If you can't connect to a remote MySql database, this is due to permissions by the hosting company/server. You can solve this by adding permission for your ip.

Unable to connect MSQL on Static IP Remotely

I am trying to connect to Mysql from java
I am using Mysql 5.5 and I can access it remotely within the local network.But when I put that System on Static Ip I can't connect to Mysql remotely.
I able to connect to Mysql port and I already check for is firewall block that port but no such issue was found.
I also checked using Toad.
Any special setting need for that please help me..
Thanks in advance.
// Edited
Error Message
ERROR 2003 (HY000): Can't connect to MySQL server on 'Static_IP' (10061)
Hi can you please share stack trace of exception
Try Following solutions :
Make sure that MySQL Conncetor for java is included in your class path.
Try this sample code :
Declare class variables :
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://ip_address:port_name/db_name";
static final String USER = "username";
static final String PASS = "password";
Sample code to create database connection :
Connection conn = null;
Statement stmt = null;
// STEP 1: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 2: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 3: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM table_name";
ResultSet rs = stmt.executeQuery(sql);
Then iterate rs to fetch the rows

Categories