Please follow the below Steps.
1) Create a 'contact' Table in Mysql 'Test' database
CREATE TABLE `contact` (
`Id` bigint(20) NOT NULL,
`Name` varchar(30) DEFAULT NULL,
`Email` varchar(20) DEFAULT NULL,
)
2) create a folder/package and rename it as "JDBC" copy as below this java code and save it as "JDBC1.java".
package JDBCTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBC1 {
public static void main(String args[]) throws Exception{
Connection con= null;
ResultSet rs = null;
PreparedStatement psmt = null;
Integer Id=2005;
String Name ="Aman";
String Email ="aman@gmail.com";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","ABCD");
psmt = con.prepareStatement("select * from contact");
rs = psmt.executeQuery();
while(rs.next())
{
System.out.println("Id is: "+rs.getInt("Id")+" name is: "+ rs.getString("Name")+" Email is: "+rs.getString("Email"));
}
psmt = con.prepareStatement("insert into contact (Id,Name,Email) values(?,?,?)");
psmt.setInt(1,Id);
psmt.setString(2,Name);
psmt.setString(3,Email);
int chk = psmt.executeUpdate();
if(chk>0){
System.out.println("\ndata has been inserted successgully auth table..");
}
else{System.out.println("Data Insertion error..");}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
rs.close();
psmt.close();
con.close();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBC1 {
public static void main(String args[]) throws Exception{
Connection con= null;
ResultSet rs = null;
PreparedStatement psmt = null;
Integer Id=2005;
String Name ="Aman";
String Email ="aman@gmail.com";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","ABCD");
psmt = con.prepareStatement("select * from contact");
rs = psmt.executeQuery();
while(rs.next())
{
System.out.println("Id is: "+rs.getInt("Id")+" name is: "+ rs.getString("Name")+" Email is: "+rs.getString("Email"));
}
psmt = con.prepareStatement("insert into contact (Id,Name,Email) values(?,?,?)");
psmt.setInt(1,Id);
psmt.setString(2,Name);
psmt.setString(3,Email);
int chk = psmt.executeUpdate();
if(chk>0){
System.out.println("\ndata has been inserted successgully auth table..");
}
else{System.out.println("Data Insertion error..");}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
rs.close();
psmt.close();
con.close();
}
}
}
3) download this jar "mysql-connector-java-3.1.7-bin.jar" in this url: http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjava5114binjar.htm
4) create a folder/package and rename it "sqljar" and put the jar in it.
5) copy the below code and save it as "Run.bat"
javac -cp "sqljar\*;." -d . JDBC1.java
java -cp "sqljar\*;." JDBCTest.JDBC1
pause;