Welcome To Uday Satya Blog

101- 110 PROGRAMS



101  Program to Check JDBC Connection

import java.sql.*;
import java.util.*;
public class JDBCDemo1
{
public static void main(String[]args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:MyDSN");
System.out.println("Connection Successfull");
c.close();
}
}


102   Program to retrieve Records from Database

import java.sql.*;
public class TestRetrieveRecords {
   public static void main(String[] args) throws Exception {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con=DriverManager.getConnection("jdbc:odbc:mdbTEST");
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from emp");
      System.out.println("*** Employee's Details *** \n");
       while(rs.next())  {
         System.out.println("Employee Number: " + rs.getInt(1));
         System.out.println("Employee Name: " + rs.getString(2));
         System.out.println("Employee Salary: " + rs.getInt(3) + "\n");
       }
     rs.close();
      st.close();
      con.close();
    } }

OUTPUT :
*** Employee's Details ***

Employee Number: 1
Employee Name: Uday
Employee Salary: 6000

Employee Number: 2
Employee Name: Venkat
Employee Salary: 4500

Employee Number: 3
Employee Name: Satish
Employee Salary: 7000

Employee Number: 4
Employee Name: Vijay
Employee Salary: 2000


103  Program to demonstrate Insertion into Database

import java.sql.*;
import java.io.*;
public class InsertDemo {
   public static void main(String[] args) throws Exception {
      int no=9;
      String name="Satya";
      double sal=10000;
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:mdbTEST");
      Statement st=con.createStatement();
            st.executeUpdate("insert into Emp values("+ no + ",'"+ name +"',"+ sal +")");
      System.out.println("*** Record Inserted Successfully ***\n");
      st.close();
      con.close();
    } }


104  Program to demonstrate Update into Database

import java.util.*;
import java.sql.*;
import java.io.*;
public class JDBCUpdateDemo {
public static void main(String[]args) throws Exception{
int eno=9;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:mdbTEST");
Statement st=c.createStatement();
int i = st.executeUpdate("update Emp set Salary = Salary +1000 where Empid="+eno);
System.out.println(i+"----record Updated");
st.close();
c.close();
}}


105  Program to demonstrate Prepared Statement
import java.sql.*;
import java.io.*;
public class JDBCPrepareDemo {
   public static void main(String[] args)throws Exception {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:mdbTEST"); 
 PreparedStatement pst = con.prepareStatement("insert into Emp(Empid,Name,Salary) values(?,?,?)");
      int no=104;
      pst.setInt(1,no);
      String name="RUVN";
      pst.setString(2,name);
      int sal=14000;
      pst.setInt(3,sal);
      int i = pst.executeUpdate();
      System.out.println(i+"--Record inserted");
      pst.close();
      con.close();
     } }


106  Program to Demonstrate Data Entry using Applet

import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class JDBCDataEntry extends Frame implements ActionListener
{
Button ok,cancel;
TextField t1,t2,t3;
JDBCDataEntry()
{
setLayout(new FlowLayout());
ok=new Button("OK");
cancel=new Button("Cancel");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
add(new Label("Employee Number  : "));
add(t1);
add(new Label("Employee Name    :  "));
add(t2);
add(new Label("Employee Salary  :       "));
add(t3);
add(ok);
add(cancel);
ok.addActionListener(this);
cancel.addActionListener(this);
setTitle("Employee's Registration Form");
setSize(350,300);
setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==cancel)
{
t1.setText("");
t2.setText("");
t3.setText("");
}

if(ae.getSource()==ok)
{
int no=Integer.parseInt(t1.getText());
String name=t2.getText();
int sal=Integer.parseInt(t3.getText());

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mdbTEST");
Statement st=con.createStatement();
st.executeUpdate("insert into Emp values("+ no + ",'" + name + "' , " + sal + ")");
System.out.println("*******Registraiton performed*******");
st.close();
con.close();
}

catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}


public static void main(String[] args) throws Exception
{
new JDBCDataEntry();
}
}
  


107  Program to Demonstrate Result Set Meta Data

import java.sql.*;
public class JDBCRSMetaDataDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:mdbTEST","scott","tiger");
String s="select * from emp";
Statement st=c.createStatement();
ResultSet rs=st.executeQuery(s);
ResultSetMetaData rsm=rs.getMetaData();
rs.next();
System.out.println("No of columns in the table: "+ rsm.getColumnCount());
System.out.println(rsm.getColumnName(1)+" : "+rsm.getColumnTypeName(1));
System.out.println(rsm.getColumnName(2)+" : "+rsm.getColumnTypeName(2));
System.out.println(rsm.getColumnName(3)+" : "+rsm.getColumnTypeName(3));
rs.close();
st.close();
c.close();
}
}

OUTPUT :

No of columns in the table: 3
Empid : INTEGER
Name : VARCHAR
Salary : INTEGER









108  Program to Demonstrate Transaction handling

import java.sql.*;

public class JDBCTransDemo
{
public static void main(String a[])
{
try{
Properties p=new Properties();
            p.setProperty("uid","scott");
            p.setProperty("password","tiger");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:MyDSN",p);
//set the auto commit mode to false
con.setAutoCommit(false);
//create a transaction
Statement st=con.createStatement();
int i= st.executeUpdate("insert into Emp values("+ no + ",'"+ name +"',"+ sal +")");
System.out.println("first row inserted but not commited");
     //create another transaction
Statement st=con.createStatement();
int j= st.executeUpdate("insert into Emp values("+ no + ",'"+ name +"',"+ sal +")");
System.out.println("second row inserted but not commited");
//commit the trans
   con.commit();
System.out.println("trans commited");
st.close();
      con.close();
}catch(Exception e)
{
System.out.println("Error: "+e);
}
}
}




109  Program to Demonstrate Callable  Statement

import java.sql.*;
import java.util.*;

public class JDBCCallableDemo
{
public static void main(String[]args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:MyDSN1");
String str= "{call emp_job(?,?,?)}";
//call a stored procedure
CallableStatement cs=c.prepareCall(str);
//pass the IN parameter
cs.setInt(1,7839);
//register the out parameters
cs.registerOutParameter(2,Types.VARCHAR);
cs.registerOutParameter(3,Types.VARCHAR);
//process the stored procedure
cs.execute();
//retrieve the data
ename=cs.getString(2);
ejob=cs.getString(3);
//display the data
System.out.println("Employee name: "+ename);
System.out.println("Employee Job: "+ejob);
cs.close();
c.close();
}
}








110  Program to Demonstrate Result Set Meta Data

import java.sql.*;
import java.util.*;
import java.io.*;
public class Rsmeta1
{
public static void main(String[]args) throws Exception
{
Properties p=new Properties();
p.setProperty("uid","scott");
p.setProperty("password","tiger");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:student",p);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from student");
ResultSetM
System.out.print(rsmd.getColumnLabel(i+1)+" ");
}
System.out.println();
while(rs.next())
{
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+" "+rs.getInt(3)+" "+rs.getInt(4));
}
st.close();
c.close();
}
}

0 comments:

Post a Comment