Font Awesome Icons
Tutorial
Menu
Core Java
AWT
Swing
Servlet
JSP
JDBC
Spring
Hibernate
JAVA JDBC
JDBC Introduction
JDBC Driver
DB Connectivity Steps
Connectivity with Oracle
Connectivity with MySQL
DriverManager
Connection
Statements
ResultSet
PreparedStatement
Store image
Retrieve image
Store file
Retrieve file
CallableStatement
Transaction Management
JDBC Basic Query
JAVA JDBC
JDBC Introduction
JDBC Driver
DB Connectivity Steps
Connectivity with Oracle
Connectivity with MySQL
DriverManager
Connection
Statements
ResultSet
PreparedStatement
Store image
Retrieve image
Store file
Retrieve file
CallableStatement
Transaction Management
JDBC Basic Query
JDBC Basic Query
1)To Create Table using prepared statement
public class CreateTable {
public static void main(String[] args) {
try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “root”);
String sql = “create table emp5(name varchar(25),department varchar(5),salary varchar(25))”;
PreparedStatement stmt = con.prepareStatement(sql);
stmt.executeUpdate(sql); con.close();
System.out.println(“table created”);
}catch(SQLException sqlex){
sqlex.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2)Insert Data in table using prepared statement
public class PSC {
public static void main(String a[]){
try {
Class.forName(“com.mysql.jdbc.Driver” );
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);
String query = “insert into emp(name,salary) values(?,?)”;
PreparedStatement prSt = con.prepareStatement(query);
prSt.setString(1, “c”);
prSt.setInt(2, 10000);
//count will give you how many records got updated
int count = prSt.executeUpdate(); //Run the same query with different values
prSt.setString(1, “CROWN1”);
prSt.setInt(2, 5000);
count = prSt.executeUpdate();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
2)Insert statement
import java.sql.*;
class Insert1{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,” system”,”oracle”);
PreparedStatement stmt=con.prepareStatement(“insert into Emp values(?,?)”); //create table emp(id number(10),name varchar2(50)); stmt.setInt(1,256);//1
stmt.setString(2,”Arun sir”);
int i=stmt.executeUpdate();
System.out.println(i+” records inserted”);
con.close();
}catch(Exception e){ System.out.println(e);
}
}
}
3)Update statement
public class Update{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);
PreparedStatement stmt=con.prepareStatement(“update emp set name=? where salary=?”);
stmt.setString(1,”Arun”);//1 specifies the first parameter in the query i.e. name
stmt.setInt(2,44444);
int i=stmt.executeUpdate();
System.out.println(i+” records updated”);
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
4) Delete statement
public class Delete{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);
PreparedStatement stmt=con.prepareStatement(“delete from emp where salary=?”);
stmt.setInt(1,5000);
int i=stmt.executeUpdate();
System.out.println(i+” records deleted”);
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
5) Select statement
public class Select{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);
PreparedStatement stmt=con.prepareStatement(“select * from emp”);
ResultSet rs=stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)+” “+rs.getInt(2));
}
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Hi, Welcome to etechvidya
Home
About Us
Tutorial