JDBC Programs P-1: Package Import Public Class Public Static Void Throws Null Null Try
JDBC Programs P-1: Package Import Public Class Public Static Void Throws Null Null Try
JDBC Programs P-1: Package Import Public Class Public Static Void Throws Null Null Try
P-1
package jdbcLabs;
import java.sql.*;
public class Lab1 {
public static void main(String args[]) throws SQLException{
Connection con=null;
Statement st=null;
try{
//1.load the driver class
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
//2.Establish the between application and database
//con=DriverManager.getConnection("jdbc:oracle:thin:@local
host:1521:XE","System","aqib");
con=DriverManager.getConnection("jdbc:mysql://localhost:9999/db1","root","arcus");
//3.prepare the sql statement
String str="insert into student
values(111,'nivya',888914444)";
//4.prepare jdbc statement
st=con.createStatement();
//5.submit sql quarry to database by using jdbc statement
int x=st.executeUpdate(str);
//6.processing the result
if(x==1){
System.out.println("Record is inserted");
}else{
System.out.println("Record is not inserted");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
P-2
package jdbcLabs;
import java.sql.*;
public class lab2 {
con=DriverManager.getConnection("jdbc:mysql://localhost:9999/db1","root","arcus");
String str="select * from student";
st=con.createStatement();
rs=st.executeQuery(str);
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
long phone=rs.getLong(3);
System.out.println(sid+ "\t" +sname+ "\t" +phone);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(rs!=null)rs.close();
if(st!=null)st.close();
if(con!=null)con.close();
}
catch(Exception e) {
e.printStackTrace();
p-3
package jdbcLabs;
import java.sql.*;
public class jdbcUtil {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex) {
System.out.println(ex);
}
}
public static Connection getMySqlConnection() throws SQLException {
String url= "jdbc:mysql://localhost:9999/db1";
Connection con = DriverManager.getConnection(url, "root", "arcus");
return con;
}
public static void cleanup(Connection con, Statement st) {
try {
if(con!=null)
con.close();
if(st!=null)
st.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public static void cleanup(Connection con, Statement st, ResultSet rs) {
try {
if(con!=null)
con.close();
if(st!=null)
st.close();
if(rs!=null)
rs.close();
}
catch(Exception e) {
System.out.println(e);
}
CONT…..
package jdbcLabs;
import java.sql.*;
public class Lab3 {
public static void main(String args[]) throws SQLException {
Connection con=null;
Statement st=null;
try {
con=jdbcUtil.getMySqlConnection();
String str="insert into student values(16,'shri',665798988)";
st=con.createStatement();
int x=st.executeUpdate(str);
if(x==1) {
System.out.println("Record is inserted");
}
else {
System.out.println("Record is not inserted");
}
} catch(Exception e) {
System.out.println(e);
e.printStackTrace();
}
jdbcUtil.cleanup(con, st);
}
}
p-4
package jdbcLabs;
import java.sql.*;
public class Lab4 {
con=DriverManager.getConnection("jdbc:mysql://localhost:9999/db1","root","arcus");
String str="select * from student where sid=1005";
st=con.createStatement();
rs=st.executeQuery(str);
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
long phone=rs.getLong(3);
System.out.println(sid+ "\t" +sname+ "\t" +phone);
}
}
catch(Exception e) {
e.printStackTrace();
}
try {
jdbcUtil.cleanup(con, st, rs);
}catch(Exception e) {
p-5
package jdbcLabs;
import java.util.*;
import java.sql.*;
public class Lab5 {
} catch(Exception e) {
e.printStackTrace();
}
}
}
p-6
package jdbcLabs;
import java.util.*;
import java.sql.*;
public class Lab6 {
p-7
package jdbcLabs;
import java.util.Scanner;
import java.sql.*;
public class Lab7 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
con=jdbcUtil.getMySqlConnection();
int sid=1888;
String name="jjjkjj";
long phone=989879;
String qry=String.format("insert into student values(%d,'%s',
%d)",sid,name,phone);
st=con.createStatement();
boolean x=st.execute(qry);
if(x){
rs=st.getResultSet();
if(rs.next()){
do{
int sid1=rs.getInt(1);
String name1=rs.getString(2);
long phone1=rs.getLong(3);
System.out.println(sid1+ "\t" +name1+ "\t" +phone1);
}
while(rs.next());
}
}
else{
int val=st.getUpdateCount();
System.out.println("result:" +val);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
p-8
package jdbcLabs;
import java.sql.*;
public class Lab8 {
}
jdbcUtil.cleanup(con, ps);
}
p-9
package jdbcLabs;
import java.sql.*;
public class Lab9 {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs= null;
try{
con=jdbcUtil.getMySqlConnection();
String str="select* from student";
ps=con.prepareStatement(str);
rs=ps.executeQuery();
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
long phone=rs.getLong(3);
System.out.println(sid+"\t"+sname+"\t"+phone);
}
} catch(Exception e) {
System.out.println(e);
}
jdbcUtil.cleanup(con, ps);
}
}
p-10
package jdbcLabs;
import java.sql.*;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs= null;
try{
con=jdbcUtil.getMySqlConnection();
// int sid=2017;
// String name="nivya";
// long phone=969558988;
// String qry=String.format("insert into student values(%d,'%s',
%d)",sid,name,phone);
String qry="select * from student ";
ps=con.prepareStatement(qry);
boolean x=ps.execute();
if(x) {
rs=ps.getResultSet();
if(rs.next())
{
do{
int sid1=rs.getInt(1);
String name1=rs.getString(2);
long phone1=rs.getLong(3);
System.out.println(sid1+"\t"+name1+"\t"+
phone1);
} while(rs.next());
}
else {
int val=ps.getUpdateCount();
System.out.println("result:"+val);
}
catch(Exception e){
e.printStackTrace();
}
p-11
package jdbcLabs;
import java.sql.*;
public class Lab11 {
p-12
package jdbcLabs;
import java.sql.*;
public class Lab12 {
}
catch(Exception e) {
e.printStackTrace();
}
finally{
jdbcUtil.cleanup(con, st);
}
p-13
package jdbcLabs;
import java.sql.*;
}
catch(Exception e) {
e.printStackTrace();
}
finally{
jdbcUtil.cleanup(con, ps);
}
p-14
package jdbcLabs;
import java.sql.*;
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
jdbcUtil.cleanup(con, st,rs);
}
catch(Exception e){
e.printStackTrace();
}
}
}
p-15
package jdbcLabs;
import java.sql.*;
p-16
package jdbcLabs;
import java.sql.*;
public class Lab16 {
}
catch(Exception e) {
e.printStackTrace();
}
p-17
package jdbcLabs;
import java.sql.*;
public class Lab17 {
}
}