Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of gudii9
gudii9🇺🇸

insert into mysql database from java program
Hi,

I am trying below database table insert example to MySql database.



import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.SQLException;

//import com.mysql.PreparedStatement;

import com.mysql.jdbc.PreparedStatement;


public class DBConnect {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
try {
	Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/MySql","root","root");

String queryy="insert into xyz_table values(?,?,?)";
System.out.println("queryy is"+queryy);
	
	PreparedStatement psmt=(PreparedStatement) conn.prepareStatement(queryy);
	//psmt.setp
	psmt.setPersonid(1,111);
	psmt.setfirstname(2,"sss");
	psmt.setage(3,333);
	psmt.executeUpdate();
	
	
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	}
	

}

Open in new window

I am able to ping database successflly and able to see created table results successfully. When I try to insert from java program getting errorss as below after commenting below lines
      psmt.setPersonid(1,111);
      psmt.setfirstname(2,"sss");
      psmt.setage(3,333);

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySql
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at DBConnect.main(DBConnect.java:19)

I do have mysql jdbc jar in the build path.

i also see when i comment below lines
      psmt.setPersonid(1,111);
      psmt.setfirstname(2,"sss");
      psmt.setage(3,333);

getting errors

The method setPersonid(int, int) is undefined for the type PreparedStatement

I also see messages like add cast to psmt etc.

I see two import options for PreparedStatement one from SQL other from SQL.jdbc package. what are the difference between those two.
I am getting error there like

Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement


 Which one should i select.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance


i modified the code as below

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//import java.sql.PreparedStatement;
import java.sql.SQLException;

//import com.mysql.PreparedStatement;

//import com.sql.PreparedStatement;


public class DBConnect {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
try {
	Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/MySql","root","root");

String queryy="insert into xyz_table values(?,?,?)";
System.out.println("queryy is"+queryy);
	
	PreparedStatement psmt=conn.prepareStatement(queryy);
			//psmt.setp
	//psmt.setPersonid(1,111);
	psmt.setInt(1,111);
	psmt.setString(2,"sss");
	//psmt.setfirstname(2,"sss");
	psmt.setDouble(3,333);
	//psmt.setage(3,333);
	psmt.executeUpdate();
	
	System.out.println("connected");
	System.out.println("inserted well");
	
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	}
	

}

Open in new window


i still get error as below
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySql
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at DBConnect.main(DBConnect.java:20)

please advise


when i tried below example
http://stackoverflow.com/questions/9353170/access-mysql-database-in-eclipse-using-java

it worked fine

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

import com.mysql.jdbc.Statement;

public class OrderData {
public static void main(String[] args)throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MySql","root","root");
    Statement stmt = (Statement) con.createStatement();
//    String insert = "INSERT INTO customer(name,C_ID,address,email)      VALUES (a,5,b,c)";
    String insert = "INSERT INTO customer(name,C_ID,address,email) VALUES ('aaa',11,'bbb','ccc');";
    stmt.executeUpdate(insert);
    System.out.println("inseertd");
    
}
}

Open in new window


not sure what is the issue with earlier example i was trying which is using preparedstatement instead of statement.
strangely below example also worked fine
http://alvinalexander.com/java/java-mysql-insert-example-preparedstatement
Please advise
SELECCTTable.jpg
PING.jpg
PSMT.jpg
mysql-JAR.jpg
Undefined.jpg
Undefined2.jpg
Undefined3.jpg
psmtErorr.jpg
OrderDB.jpg
OrderDB2.jpg
users.jpg

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of CEHJCEHJ🇬🇧

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySql
Is 'MySql' the name of the database? Can you connect to it by name with the command line client?
I do have mysql jdbc jar in the build path.
You need it in the classpath

Avatar of gudii9gudii9🇺🇸

ASKER

>>Is 'MySql' the name of the database? Can you connect to it by name with the command line client?

yes I am able to connect to MySql database and see xyx_table under that.


>>You need it in the classpath

How do I set class path . Please advise

Avatar of CEHJCEHJ🇬🇧

yes I am able to connect to MySql database and see xyx_table under that.
Can you show me a successful command line command that you use for that?

How do I set class path . Please advise
Depends on your environment. What IDE are you using (if any)?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of gudii9gudii9🇺🇸

ASKER

I am using Eclipse Juno version. I am attaching the screenshot. When queries with select statement it says empty set as the table is empty. please advise
EmptySet.jpg

Avatar of gudii9gudii9🇺🇸

ASKER

Even pulling the data i am getting same error



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.PreparedStatement;
import java.sql.SQLException;

//import com.mysql.PreparedStatement;

//import com.sql.PreparedStatement;


public class DBRetrieve {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
try {
      Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/MySql","root","root");

String queryy="select * from users";
//System.out.println("queryy is"+queryy);
      
      PreparedStatement psmt=conn.prepareStatement(queryy);
                  //psmt.setp
      //psmt.setPersonid(1,111);
      ResultSet rs=psmt.executeQuery();
      int count=0;
      while(rs.next()){
            
            
            System.out.println(rs.getInt(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
            System.out.println(rs.getDate(4));
            System.out.println(rs.getBoolean(5));
            System.out.println(rs.getInt(6));
            //System.out.println(rs.getInt(6));
      }

      System.out.println("connected");
      //System.out.println("inserted well");
      
} catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
}
      }
      

}

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySql
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at DBRetrieve.main(DBRetrieve.java:21)

please advise

SOLUTION
Avatar of CEHJCEHJ🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

ASKER CERTIFIED SOLUTION
Avatar of mccarlmccarl🇦🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.