I want to create my database using pure sql code from sql command line sql command line imageand want to connect to the database from java code(jdbc). But while doing so I am facing problems- here is what I have tried: I have set the path of the driver - ["mysql-connector-java-5.1.39-bin"-a jar file ]- in path variable as well as ,on some suggestions from internet, I added this jar file to the [C:\Program Files\Java\jdk1.8.0\jre\bin] folder. but when I run my program from command prompt it shows an Exception - the image - message from command prompt. I have created a user for the databse from sql command prompt too. here is my java code -
import java.sql.*;
public class JDBCDemo
{
public static void main(String... args)
{
try(Connection conn = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/studentdb?useSSL=false","amir","amir5");
// step 2 . allocate a statement object in the connection
Statement stmt = conn.createStatement();
) {
//step 3. execute a SQL select query, the query result
String strSelect = "select * from student;";
System.out.println("The sql query is "+ strSelect);
System.out.println();
ResultSet rset = stmt.executeQuery(strSelect);
System.out.println("the records are selected");
int rowCount =0;
while(rset.next())
{
String name = rset.getString("name");
String roll = rset.getString("roll_num");
int id = rset.getInt("id");
System.out.println(name+", "+roll+", "+id);
++rowCount;
}
System.out.println(rowCount);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
how can I get rid of this problem?
4 Answers 4
Given the error message
java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/studentdb?useSSL=false
your connection string is missing the colon after "mysql". It should be
jdbc:mysql://localhost ...
1 Comment
You should use Class.forName("com.mysql.jdbc.Driver");
Refer tutorial here
Note: Download the mysql-connector-java-5.1.23-bin.jar here. It is not mentioned in that tutorial. Then set the class path where you download it.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
8 Comments
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb","amir","amir5");
use this line it will work perfectly
1 Comment
do not look here and there! just set up your mysql command line and install netbeans . netbeans has inbuilt Drivers. create a database in mysql command line then create a project in netbeans and double click on the library folder and select add library, a screen will pop up , scroll down it and select mysql odbc driver and you are done!!write your code and execute queries.