Friday, 27 September 2013

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();
}
}
}

No comments:

Post a Comment