1

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

0

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 ...
Sign up to request clarification or add additional context in comments.

1 Comment

please tell me the steps in the form of an algorithm,if possible. right from •installing mysql (mention the link , where to download from , if possible), to •writing jdbc program. that may help me understand where I have been doing wrong!!
0

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

it doesn't work- command prompt message - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
@Amir Hasan : Have you referred the tutorial as i mentioned?
yes, I did. I had copied the tutorial in fact the database name too.
Download the jar file here java2s.com/Code/Jar/m/Downloadmysqlconnectorjava5123binjar.htm. Then set the class path as mentioned in the tutorial and try it.
@Amir Hasan : Are you using Windows or Linux OS?
|
0

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb","amir","amir5");

use this line it will work perfectly

1 Comment

please tell me the steps in the form of an algorithm,if possible. right from •installing mysql (mention the link , where to download from , if possible), to •writing jdbc program. that may help me understand where I have been doing wrong!!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.