JDBC Programs P-1: Package Import Public Class Public Static Void Throws Null Null Try

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

JDBC PROGRAMS

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 {

public static void main(String[] args) {


Connection con=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");

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 {

public static void main(String[] args) {


Connection con=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");

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 {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
Connection con=null;
Statement st=null;
try {
con=jdbcUtil.getMySqlConnection();
System.out.println("enter id");
int sid=sc.nextInt();
System.out.println("enter name");
String name=sc.next();
System.out.println("enter phone no");
long phone=sc.nextLong();
String qry=String.format("insert into student values(%d,'%s',
%d)",sid,name,phone);
st=con.createStatement();
int x=st.executeUpdate(qry);
if(x==1) {
System.out.println("Record is inserted");
} else {
System.out.println("Record is not inserted");
}

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

}
}

p-6

package jdbcLabs;
import java.util.*;
import java.sql.*;
public class Lab6 {

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();
System.out.println("enter id");
int id=sc.nextInt();
String str=String.format("select * from student where sid=
%d", id);
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-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 {

public static void main(String[] args) throws SQLException {


Connection con=null;
PreparedStatement ps=null;
try{
con=jdbcUtil.getMySqlConnection();
String str="insert into student values(?,?,?)";
ps=con.prepareStatement(str);
ps.setInt(1, 78);
ps.setString(2,"ambi" );
ps.setLong(3, 88965374);
int x=ps.executeUpdate();
if(x==1) {
System.out.println("Record is inserted");
}
else {
System.out.println("Record is not inserted");
}
} catch(Exception e) {
System.out.println(e);

}
jdbcUtil.cleanup(con, ps);
}

p-9
package jdbcLabs;
import java.sql.*;
public class Lab9 {

public static void main(String[] args) {

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.*;

public class Lab10 {

public static void main(String[] args) {

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 {

public static void main(String[] args) {


Connection con=null;
CallableStatement cs=null;
try {
con=jdbcUtil.getMySqlConnection();
int id=10000;
String qry="call update student Result(?)";
cs=con.prepareCall(qry);
cs.setInt(1,id);
cs.execute();
System.out.println("procedure is submited successfully");
}
catch(Exception e) {
System.out.println("error in calling procedure");
e.printStackTrace();

p-12
package jdbcLabs;
import java.sql.*;
public class Lab12 {

public static void main(String[] args) throws SQLException {


Connection con=null;
Statement st=null;
try {
con=jdbcUtil.getMySqlConnection();
st=con.createStatement();
String qry1="insert into student values(188,'deepu',764588778)";
st.addBatch(qry1);
String qry2="insert into student values(177,'manu',7645887000)";
st.addBatch(qry2);
String qry3="delete from student where sid=111";
st.addBatch(qry3);
String qry4="update student set phone=80000 where sid=78";
st.addBatch(qry4);
int x[]=st.executeBatch();
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);

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

}
finally{
jdbcUtil.cleanup(con, st);
}

p-13
package jdbcLabs;

import java.sql.*;

public class Lab13 {

public static void main(String[] args) throws SQLException {


Connection con=null;
PreparedStatement ps=null;
try {
con=jdbcUtil.getMySqlConnection();
String qry="insert into student values(?,?,?)";
ps=con.prepareStatement(qry);
ps.setInt(1, 707);
ps.setString(2, "ghhhh");
ps.setLong(3, 6566);
ps.addBatch();
ps.setInt(1, 9041);
ps.setString(2, "hari");
ps.setLong(3, 8989);
ps.addBatch();
int x[]=ps.executeBatch();
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);

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

}
finally{
jdbcUtil.cleanup(con, ps);
}

p-14
package jdbcLabs;
import java.sql.*;

public class Lab14 {

public static void main(String[] args) {


Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
con=jdbcUtil.getMySqlConnection();
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{
jdbcUtil.cleanup(con, st,rs);
}
catch(Exception e){
e.printStackTrace();
}
}
}

p-15
package jdbcLabs;

import java.sql.*;

public class Lab15 {

public static void main(String[] args) throws SQLException {


Connection con=jdbcUtil.getMySqlConnection();
DatabaseMetaData md=con.getMetaData();
System.out.println("url:"+md.getURL());
System.out.println("user name:"+md.getUserName());
System.out.println("major version:"+md.getDatabaseMajorVersion());
System.out.println("minor version:"+md.getDatabaseMinorVersion());
System.out.println("full join:"+md.supportsFullOuterJoins());
System.out.println("database product
name:"+md.getDatabaseProductName());

p-16
package jdbcLabs;
import java.sql.*;
public class Lab16 {

public static void main(String[] args) throws SQLException {


Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
con=jdbcUtil.getMySqlConnection();
String str="select * from student";
st=con.createStatement();
rs=st.executeQuery(str);
ResultSetMetaData rmd=rs.getMetaData();
//int cols=rmd.getColumnCount();
for(int i=1;i<=rmd.getColumnCount();i++) {
String str1=rmd.getColumnName(i);
String str2=rmd.getColumnName(i);
String str3=rmd.getColumnTypeName(i);
int size=rmd.getPrecision(i);
System.out.print(str1+"\t"+str2+"\t"+str3+"\t"+size);
}

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

p-17
package jdbcLabs;

import java.sql.*;
public class Lab17 {

public static void main(String[] args) {


Connection con=null;
Statement st=null;
try{
con=jdbcUtil.getMySqlConnection();
con.setAutoCommit(false);
String qry1="insert into student values(1,'ashu',7989888)";
st=con.createStatement();
st.execute(qry1);
String qry2="insert into student values(20,'shrey',777777)";
st=con.createStatement();
st.execute(qry2);
con.commit();
System.out.println("trancation inserted successsfully");
}
catch(Exception e) {
try{
con.rollback();
System.out.println("trancation not inserted
successsfully");
}
catch(Exception ee) {

}
}

You might also like