Friday, 27 September 2013

JDBC CURD Operations using PreparedStatement


//Insert Record into table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="insert into emp values(?,?)";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);
pstmt.setString(2,"jamesGosling");
pstmt.executeUpdate();

System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Update Record/Records in the table using PreparesStatement in JDBC


package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="update emp set name=? where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setString(1,"java");
pstmt.setInt(2,25);

pstmt.executeUpdate();

System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Delete Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="delete from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);

int count=pstmt.executeUpdate();

System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}



//Retrieve Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="select * from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,26);

ResultSet rs=pstmt.executeQuery();

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

JDBC CURD operations using Statement object


//Create table using Statement object in JDBC

package com.iton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "create table emp(no int,name varchar(15))";

stmt.executeUpdate(query);
System.out.println("+++Table is created successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Insert Record into table using Statement object in JDBC

package com.iton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "insert into emp values(25,'jamesGosling')";

stmt.executeUpdate(query);
System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Update Record/Records in the table using Statement object in JDBC

package com.iton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "update emp set name='james' where no=25";

stmt.executeUpdate(query);
System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Delete Record/Records in the table using Statement object in JDBC

package com.iton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "delete from emp where no=25";

int count=stmt.executeUpdate(query);
System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Retrieve Record/Records in the table using Statement object in JDBC

package com.iton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "select * from emp";

ResultSet rs=stmt.executeQuery(query);

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}