Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program 1
- /*1.Write a JDBC program to fetch all the records from following Emp table.
- FieldName Datatype
- Emp_Id Text
- Emp_Name Text
- Salary Number
- Desig Text
- Department Text
- Dept_No Text
- *
- */
- import java.sql.*;
- public class Prg1
- {
- public static void main(String str[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- String url ="jdbc:odbc:Assign3";
- Connection con = DriverManager.getConnection(url);
- Statement st = con.createStatement();
- ResultSet rs =st.executeQuery("select * from Emp");
- while(rs.next())
- {
- System.out.println("Emp No : "+ rs.getString(1));
- System.out.println("Emp Name : " + rs.getString(2));
- System.out.println("Emp Salary : " + rs.getInt(3));
- System.out.println("Emp Designation : "+ rs.getString(4));
- System.out.println("Emp Department : " + rs.getString(5));
- System.out.println("Emp Department No : "+ rs.getString("Dept_No"));
- }
- con.close();
- }
- catch(Exception e)
- {
- System.out.println("Exception Occured");
- }
- }
- } Program 2 - Part 1
- /*
- *
- 2. Write a Simple JDBC program to insert, update, delete and search records
- using switch case
- */
- import java.sql.*;
- import java.io.*;
- public class Prg2
- {
- public static void main(String args[])
- {
- Connection con=null;
- try
- {
- System.out.println("1. Insert ");
- System.out.println("2. Update");
- System.out.println("3. Delete");
- System.out.println("4. Search");
- System.out.println("\n\nEnter your choice :");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int choice = Integer.parseInt(br.readLine());
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- con = DriverManager.getConnection("jdbc:odbc:assign3");
- switch(choice)
- {
- case 1:
- System.out.println("Enter No :");
- String no = br.readLine();
- System.out.println("Enter name :");
- String name = br.readLine();
- PreparedStatement pst = con.prepareStatement("insert into table1 values("+no+",'"+ name +"')");
- pst.executeUpdate();
- System.out.println("Data is inserted");
- break;
- case 2:
- System.out.println("Enter No. for update :");
- int no_update =Integer.parseInt(br.readLine());
- System.out.println("Enter New Number :");
- int new_no = Integer.parseInt(br.readLine());
- System.out.println("Enter new name :");
- String new_name = br.readLine();
- Program 2 - Part 2
- pst = con.prepareStatement("update Table1 set Table1.no="+ new_no +", Table1.name='" + new_name + "' where Table1.no=" + no_update );
- pst.executeUpdate();
- System.out.println("Row updated");
- //System.out.println("update Table1 set no="+ new_no +", name='" + new_name + "' where no=" + no_update );
- break;
- case 3:
- System.out.println("Enter num for delete that row :");
- int no_delete = Integer.parseInt(br.readLine());
- pst = con.prepareStatement("Delete from table1 where table1.no = "+ no_delete);
- pst.executeUpdate();
- System.out.println("Row deleted");
- break;
- case 4:
- System.out.println("Enter no to search the name :");
- int no_search = Integer.parseInt(br.readLine());
- pst = con.prepareStatement("select name from table1 where table1.no =" + no_search);
- ResultSet rs = pst.executeQuery();
- rs.next();
- System.out.println("Name : "+ rs.getString("name"));
- break;
- default:
- System.out.println("Invalid CHoice");
- }
- con.close();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- Program 3
- /*
- * 3. Write a JDBC program to create an entry form
- */
- import java.sql.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- public class Prg3 extends Applet implements ActionListener
- {
- Label lno , lname,lalert;
- TextField tno,tname;
- Button bInsert;
- public void init()
- {
- lno = new Label("Enter no :");
- tno = new TextField(10);
- lname = new Label("Enter name :");
- tname = new TextField(15);
- bInsert = new Button("Insert");
- lalert = new Label(" ");
- add(lno);
- add(tno);
- add(lname);
- add(tname);
- add(bInsert);
- add(lalert);
- bInsert.addActionListener(this);
- }
- public void actionPerformed(ActionEvent ae)
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
- Statement st = con.createStatement();
- st.executeUpdate("insert into table1 values("+ tno.getText() + ",'"+ tname.getText() + "')");
- tno.setText("");
- tname.setText("");
- lalert.setText("Data Inserted");
- con.close();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- program 4 - Part 1
- /*
- * 4. Write a JDBC program for Record Navigation
- */
- import java.sql.*;
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- public class Prg4 extends Applet implements ActionListener
- {
- Label lno,lname;
- TextField tno , tname;
- Button next,previous,last,first;
- Connection con=null;
- ResultSet rs;
- Statement st;
- public void init()
- {
- lno = new Label("No : ");
- tno = new TextField(10);
- lname = new Label("Name :");
- tname = new TextField(10);
- next = new Button("Next");
- previous = new Button("Previous");
- last = new Button("Last");
- first = new Button("First");
- add(lno);
- add(tno);
- add(lname);
- add(tname);
- add(first);
- add(next);
- add(previous);
- add(last);
- first.addActionListener(this);
- next.addActionListener(this);
- previous.addActionListener(this);
- last.addActionListener(this);
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- con = DriverManager.getConnection("jdbc:odbc:assign3");
- st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
- rs = st.executeQuery("select * from table1");
- }
- catch(Exception e)
- {
- }
- }
- program 4 - Part 2
- public void actionPerformed(ActionEvent ae)
- {
- try
- {
- if(ae.getSource() == first)
- {
- rs.first();
- }
- if(ae.getSource() == next)
- {
- rs.next();
- if(rs.isAfterLast())
- {
- rs.last();
- }
- }
- if(ae.getSource() == previous)
- {
- rs.previous();
- if(rs.isBeforeFirst())
- {
- rs.first();
- }
- }
- if(ae.getSource() == last)
- {
- rs.last();
- }
- tno.setText(rs.getString("no"));
- tname.setText(rs.getString("name"));
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public void destroy()
- {
- try
- {
- con.close();
- }
- catch(Exception e)
- {}
- }
- }
- program 5
- /*
- * 5. Write a JDBC program which search the record and display it in to the JTable (Using Storeprocedure)
- */
- import java.sql.*;
- import javax.swing.*;
- import javax.swing.table.*;
- import java.awt.event.*;
- import java.awt.*;
- public class Prg5 extends JFrame
- {
- JLabel lno;
- JTextField tno;
- JButton search;
- JTable table;
- DefaultTableModel model;
- public Prg5()
- {
- String[][] data={};
- String[] fields = {"No" , "Name"};
- JPanel p1 = new JPanel(new FlowLayout());
- lno = new JLabel("Enter No :");
- tno = new JTextField(10);
- search = new JButton("Search");
- model = new DefaultTableModel(data , fields);
- table = new JTable(model);
- p1.add(lno);
- p1.add(tno);
- p1.add(search);
- JScrollPane pane = new JScrollPane(table);
- getContentPane().add(p1);
- search.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery("select * from table1 where table1.no="+tno.getText());
- while(rs.next())
- {
- Object[] obj = {rs.getString(1),rs.getString(2)};
- model.addRow(obj);
- }
- }
- catch(Exception p)
- {
- }
- }
- });
- }
- public static void main(String args[])
- {
- Prg5 jt = new Prg5();
- jt.setLayout(new GridLayout(2,1) );
- //jt.pack();
- jt.setSize(300, 300);
- jt.setVisible(true);
- }
- }
- /* This program is done using ACCESS insted of ORACLE...is u want to do it with ORACLE then try with ur self..*/
- Program 6
- /*
- 6. Write a JDBC program to use ResultSetMetaData
- */
- import java.sql.*;
- public class Prg6
- {
- public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery("select * from table1");
- ResultSetMetaData rsm = rs.getMetaData();
- System.out.println("Name of Table :" + rsm.getTableName(1));
- System.out.println("Number of columns in table :" + rsm.getColumnCount());
- System.out.println("First Column Name :" + rsm.getColumnName(1));
- System.out.println("First COlumn Type :" + rsm.getColumnTypeName(1));
- System.out.println("Second Column Name :"+ rsm.getColumnName(2));
- System.out.println("Second COlumn Type :" + rsm.getColumnTypeName(2));
- System.out.println("No. Column have Auto Increment :" + rsm.isAutoIncrement(1));
- con.close();
- }
- catch(Exception e)
- {
- }
- }
- } program 7
- /*
- * 7. Write a JDBC program to use DatabaseMetaData
- */
- import java.sql.*;
- public class Prg7
- {
- public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
- DatabaseMetaData dmd = con.getMetaData();
- System.out.println("DatabaseProductName :" + dmd.getDatabaseProductName());
- System.out.println("DatabaseProductVersion :"+ dmd.getDatabaseProductVersion());
- System.out.println("DriverName :" + dmd.getDriverName());
- System.out.println("DriverVersion :" + dmd.getDriverVersion());
- //System.out.println("CatalogSeparator :"+ dmd.getCatalogSeparator());
- //System.out.println("SQLKeywords :"+ dmd.getSQLKeywords());
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement