No suitable driver found for jdbc:mysql//localhost:3306/test - java

I am trying to write a java program connecting to a MySQL database. But I am getting this error:
No suitable driver found for jdbc:mysql//localhost:3306/test
I have already installed mysql-connector-java.5.1.23-bin.jar. I am still getting this error.
Here is my program:
package database_practice;
import java.sql.*;
public class CreateConnection {
private Connection conn = null;
public static void main(String args[]) {
CreateConnection conn = new CreateConnection();
conn.getConnection();
}
public Connection getConnection() {
try {
String url = "jdbc:mysql//localhost:3306/test";
String username = "root";
String password = "admin1234";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, username, password);
System.out.println("Database Created...!!!");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error occured while connecting to database");
}
return conn;
}
}

Your connection string is wrong. This:
"jdbc:mysql//localhost:3306/test"
should be:
"jdbc:mysql://localhost:3306/test"
Note the colon after "mysql".

Related

Android studio can't connect to database in Azure sql server

I am using android studio to develop an application and using Azure Sql Server to host my database. The problem is I was able to connect to my database on SQL server but it has an error of Object not found in my database.
I found out that it might be connecting to my master database instead of the database I want it to connect to. Is there any solution to solve the problem?
package com.example.lenovo.testing1;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.*;
public class ConnectionClass {
String hostName = "haozailai.database.windows.net";
String dbName = "haozailai";
String user = "username";
String password = "password";
#SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String ConnURL;
Connection conn = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = String.format("jdbc:jtds:sqlserver://haozailai.database.windows.net:1433;database=haozailai;user=username;password=password;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
conn = DriverManager.getConnection(url);
}catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return conn;
}
}
Picture of my database structure
I tried to connect my sqlserver via java jdbc and did not reproduce your issue.
I can connect to my application db successfully.
My test code:
import java.sql.*;
public class Test {
public static final String url = "jdbc:sqlserver://***.database.windows.net:1433;database=***;user=***password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
public static final String name = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static Connection conn = null;
public static PreparedStatement pst = null;
public static Statement stmt = null;
public static ResultSet rs = null;
public static void main(String[] args) {
try {
String SQL = "select * from dbo.Student";
Class.forName(name);
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString("name"));
}
close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close() {
try {
conn.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
After some research, I found out it is because of your connect url.
You need to modify your connect url :
jdbc:jtds:sqlserver://haozailai.database.windows.net:1433/<your application db name> ...
You could refer to the pages below for more details.
https://sourceforge.net/p/jtds/discussion/104389/thread/a672d758/
how to connect sql server using JTDS driver in Android
Update answer:
I have made a slight adjustment to your connect URL and can connect to my application database normally.
try {
String SQL = "select * from dbo.Student";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = String.format("jdbc:jtds:sqlserver://***.database.windows.net:1433/<your database name>;user=***;password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString("name"));
}
close();
} catch (Exception e) {
e.printStackTrace();
}
Notice that remove the database=*** and add "/<your database name>" after your host string.
Please refer to the above code and try again.Any concern, please let me know.
Hope it helps you.
I know this answer is waay too late, but I found this video that totally works: https://www.youtube.com/watch?v=WJBs0zKGqH0
The thing is, you have to download a jtds jar, the guy in the video says where you can get it from and also, you need to add "jtds" before "sqlserver" in connection url and edit the way the 'databe' is written in the connection url, like this:
jdbc:jtds:sqlserver://serverName.database.windows.net:portNr:DatabaseName=dbName;user=....

Unable to connect MS SQL server via Java Class

I am getting error when I try to connect database
Error:com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to
Ptakip.Connection
Ptakip is my Package
Connection is my Class
Here is the Connection Class Code ;
import java.sql.*;
public class Connection {
private Connection cn;
public Connection connector( )
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection cn = (Connection)
DriverManager.getConnection("jdbc:sqlserver://localhost\\MyServer:
1433;databaseName=TEST;user=Glassfish;password=pass;");
System.out.println("connected");
}
catch(Exception ex) {
System.out.println("Error:" + ex.getMessage());
System.out.println(cn);
}
return cn;
}
}
your class has the same name as the class in the package java.sql that's why u have this conflict ,juste try to change the name to Connexion ,it should work
Try to make a connection like this. it may help you
String url = "jdbc:mysql://localhost:3306/";
String dbName = "demo”
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "mypasswd";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}

how to access connection object in another class in java?

i am using xampp mysql, this code is for JDBC program. actually there are two class one is dbconnect.java and another is login.java. I want to access the connection object (i.e. conn) in another class(i.e. login.java). But i don't have proper idea, i have included the code here please suggest me what is the problem and what are the solutions?
This is the code of dbconnect.java
package stundentrecord;
import java.sql.Connection;
import java.sql.DriverManager;
public class dbconnect {
public void conect(){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "studentRecord";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try{
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
if(con==null){
System.out.println("Connection cannot be established");
}
// con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
and here is the code from another class named login.java
if(source==login){
if(username!=null && password!=null) {
Connection conn= null;
Statement stmt = null;
dbconnect db = new dbconnect();
db.conect();
String query = "SELECT * from userlogin";
try{
stmt=(Statement) conn.createStatement(); // here is the problem
ResultSet rs = stmt.executeQuery(query); // here is the problem
while (rs.next()) {
String user = rs.getString("username");
String pass=rs.getString("password");
System.out.println("Welcome "+user);
}
} catch(SQLException ex){
ex.getMessage();
}
StundentRecord SR = new StundentRecord();
} else {
JOptionPane.showMessageDialog(null,"Username or password field is empty","error !!",JOptionPane.ERROR_MESSAGE);
}
}
What is the real problem and how to solve it?
The easiest way would be to make the connect method non void and return the connection:
public Connection conect() {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "studentRecord";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
if (con == null) {
System.out.println("Connection cannot be established");
}
return con;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
You should return your CONNECTION object from you connection class and assign it to your login class... Now your connection object is null...

Cannot understand warning message: expected:<interface java.sql.Connection> but was:<class com.mysql.jdbc.JDBC4Connection>

I am getting this message in unit test expected:<interface java.sql.Connection> but was:<class com.mysql.jdbc.JDBC4Connection>
from my code:
#Test
public void connectionTest() throws SQLException{
Connection conn = ConnectionManager.createConnection();
assertEquals(Connection.class, conn.getClass());
conn.close();
}
My mock class for getting connection:
public class ConnectionManager {
#SuppressWarnings("unused")
public static Connection createConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/library";
String name = "root";
String password = "root";
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, name, password);
} catch (ClassNotFoundException e) {
if (connection != null){
connection.close();
}
throw new SQLException(e);
}
return connection;
}
}
What is wrong with my code?
Your assert is fundamentally wrong.
Connection.class is an interface.
conn.getClass() will return a concrete class that implements that interface.

My Connection Code is not working why?

why is it my Connection code not working? I have everything right password,user, and the host and the driver but why is it not working??
import java.sql.*;
public class Connection {
public String url = "jdbc:mysql://localhost:3306/Testdb";
public String driver = "com.mysql.jdbc.Driver";
public String user = "root";
public String pass = "123192";
public void JdbcConnection(){
try{
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,user,pass);
}catch(Exception e){
e.printStackTrace();
}
}
}
The problem is that "Connection con" should be of type java.sql.Connection, but since your own class is called Connection, your code thinks you are making a reference to that instead of java.sql.Connection. You can disambiguate the type of con by using the full class name like so:
try{
java.sql.Connection con = DriverManager.getConnection(url,user,pass);
}catch(Exception e){
e.printStackTrace();
}
Please change the name of your class (it must not a Connection especially when you import the java.sql.*) and you need to specify the username and password while obtaining a connection.
public class TestConnection {
....
Connection con = DriverManager.getConnection(url,user,pass);
}

Categories