Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.21 KB | None | 0 0
  1. Program 1
  2. /*1.Write a JDBC program to fetch all the records from following Emp table.
  3.  
  4. FieldName Datatype
  5. Emp_Id Text
  6. Emp_Name Text
  7. Salary Number
  8. Desig Text
  9. Department Text
  10. Dept_No Text
  11. *
  12. */
  13.  
  14. import java.sql.*;
  15.  
  16. public class Prg1
  17. {
  18. public static void main(String str[])
  19. {
  20. try
  21. {
  22. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  23. String url ="jdbc:odbc:Assign3";
  24. Connection con = DriverManager.getConnection(url);
  25.  
  26. Statement st = con.createStatement();
  27. ResultSet rs =st.executeQuery("select * from Emp");
  28.  
  29. while(rs.next())
  30. {
  31. System.out.println("Emp No : "+ rs.getString(1));
  32. System.out.println("Emp Name : " + rs.getString(2));
  33. System.out.println("Emp Salary : " + rs.getInt(3));
  34. System.out.println("Emp Designation : "+ rs.getString(4));
  35. System.out.println("Emp Department : " + rs.getString(5));
  36. System.out.println("Emp Department No : "+ rs.getString("Dept_No"));
  37. }
  38. con.close();
  39. }
  40. catch(Exception e)
  41. {
  42. System.out.println("Exception Occured");
  43. }
  44. }
  45.  
  46. } Program 2 - Part 1
  47. /*
  48. *
  49. 2. Write a Simple JDBC program to insert, update, delete and search records
  50. using switch case
  51. */
  52. import java.sql.*;
  53. import java.io.*;
  54. public class Prg2
  55. {
  56. public static void main(String args[])
  57. {
  58. Connection con=null;
  59. try
  60. {
  61. System.out.println("1. Insert ");
  62. System.out.println("2. Update");
  63. System.out.println("3. Delete");
  64. System.out.println("4. Search");
  65. System.out.println("\n\nEnter your choice :");
  66. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  67. int choice = Integer.parseInt(br.readLine());
  68. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  69. con = DriverManager.getConnection("jdbc:odbc:assign3");
  70. switch(choice)
  71. {
  72. case 1:
  73. System.out.println("Enter No :");
  74. String no = br.readLine();
  75. System.out.println("Enter name :");
  76. String name = br.readLine();
  77. PreparedStatement pst = con.prepareStatement("insert into table1 values("+no+",'"+ name +"')");
  78. pst.executeUpdate();
  79. System.out.println("Data is inserted");
  80. break;
  81. case 2:
  82. System.out.println("Enter No. for update :");
  83. int no_update =Integer.parseInt(br.readLine());
  84. System.out.println("Enter New Number :");
  85. int new_no = Integer.parseInt(br.readLine());
  86. System.out.println("Enter new name :");
  87. String new_name = br.readLine();
  88. Program 2 - Part 2
  89. pst = con.prepareStatement("update Table1 set Table1.no="+ new_no +", Table1.name='" + new_name + "' where Table1.no=" + no_update );
  90. pst.executeUpdate();
  91. System.out.println("Row updated");
  92. //System.out.println("update Table1 set no="+ new_no +", name='" + new_name + "' where no=" + no_update );
  93. break;
  94. case 3:
  95. System.out.println("Enter num for delete that row :");
  96. int no_delete = Integer.parseInt(br.readLine());
  97. pst = con.prepareStatement("Delete from table1 where table1.no = "+ no_delete);
  98. pst.executeUpdate();
  99. System.out.println("Row deleted");
  100. break;
  101. case 4:
  102. System.out.println("Enter no to search the name :");
  103. int no_search = Integer.parseInt(br.readLine());
  104. pst = con.prepareStatement("select name from table1 where table1.no =" + no_search);
  105. ResultSet rs = pst.executeQuery();
  106. rs.next();
  107. System.out.println("Name : "+ rs.getString("name"));
  108. break;
  109. default:
  110. System.out.println("Invalid CHoice");
  111. }
  112. con.close();
  113. }
  114. catch(Exception e)
  115. {
  116. System.out.println(e);
  117. }
  118. }
  119. }
  120. Program 3
  121. /*
  122. * 3. Write a JDBC program to create an entry form
  123. */
  124.  
  125. import java.sql.*;
  126. import java.awt.*;
  127. import java.awt.event.*;
  128. import java.applet.Applet;
  129.  
  130. public class Prg3 extends Applet implements ActionListener
  131. {
  132. Label lno , lname,lalert;
  133. TextField tno,tname;
  134. Button bInsert;
  135.  
  136. public void init()
  137. {
  138. lno = new Label("Enter no :");
  139. tno = new TextField(10);
  140. lname = new Label("Enter name :");
  141. tname = new TextField(15);
  142. bInsert = new Button("Insert");
  143. lalert = new Label(" ");
  144.  
  145. add(lno);
  146. add(tno);
  147. add(lname);
  148. add(tname);
  149. add(bInsert);
  150. add(lalert);
  151.  
  152. bInsert.addActionListener(this);
  153. }
  154.  
  155. public void actionPerformed(ActionEvent ae)
  156. {
  157. try
  158. {
  159. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  160. Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
  161.  
  162. Statement st = con.createStatement();
  163.  
  164. st.executeUpdate("insert into table1 values("+ tno.getText() + ",'"+ tname.getText() + "')");
  165.  
  166. tno.setText("");
  167. tname.setText("");
  168. lalert.setText("Data Inserted");
  169. con.close();
  170.  
  171. }
  172. catch(Exception e)
  173. {
  174. System.out.println(e);
  175. }
  176. }
  177.  
  178.  
  179. }
  180. program 4 - Part 1
  181. /*
  182. * 4. Write a JDBC program for Record Navigation
  183. */
  184. import java.sql.*;
  185. import java.applet.Applet;
  186. import java.awt.*;
  187. import java.awt.event.*;
  188.  
  189. public class Prg4 extends Applet implements ActionListener
  190. {
  191. Label lno,lname;
  192. TextField tno , tname;
  193. Button next,previous,last,first;
  194. Connection con=null;
  195. ResultSet rs;
  196. Statement st;
  197.  
  198. public void init()
  199. {
  200. lno = new Label("No : ");
  201. tno = new TextField(10);
  202. lname = new Label("Name :");
  203. tname = new TextField(10);
  204.  
  205. next = new Button("Next");
  206. previous = new Button("Previous");
  207. last = new Button("Last");
  208. first = new Button("First");
  209.  
  210.  
  211. add(lno);
  212. add(tno);
  213. add(lname);
  214. add(tname);
  215. add(first);
  216. add(next);
  217. add(previous);
  218. add(last);
  219.  
  220. first.addActionListener(this);
  221. next.addActionListener(this);
  222. previous.addActionListener(this);
  223. last.addActionListener(this);
  224.  
  225. try
  226. {
  227. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  228. con = DriverManager.getConnection("jdbc:odbc:assign3");
  229.  
  230. st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  231.  
  232. rs = st.executeQuery("select * from table1");
  233. }
  234. catch(Exception e)
  235. {
  236. }
  237.  
  238.  
  239. }
  240. program 4 - Part 2
  241. public void actionPerformed(ActionEvent ae)
  242. {
  243. try
  244. {
  245.  
  246. if(ae.getSource() == first)
  247. {
  248. rs.first();
  249. }
  250.  
  251. if(ae.getSource() == next)
  252. {
  253. rs.next();
  254.  
  255. if(rs.isAfterLast())
  256. {
  257. rs.last();
  258. }
  259. }
  260.  
  261. if(ae.getSource() == previous)
  262. {
  263. rs.previous();
  264.  
  265. if(rs.isBeforeFirst())
  266. {
  267. rs.first();
  268. }
  269. }
  270.  
  271. if(ae.getSource() == last)
  272. {
  273. rs.last();
  274. }
  275. tno.setText(rs.getString("no"));
  276. tname.setText(rs.getString("name"));
  277. }
  278.  
  279.  
  280. catch(Exception e)
  281. {
  282. System.out.println(e);
  283. }
  284. }
  285.  
  286. public void destroy()
  287. {
  288. try
  289. {
  290. con.close();
  291. }
  292. catch(Exception e)
  293. {}
  294. }
  295.  
  296.  
  297. }
  298. program 5
  299. /*
  300. * 5. Write a JDBC program which search the record and display it in to the JTable (Using Storeprocedure)
  301. */
  302. import java.sql.*;
  303. import javax.swing.*;
  304. import javax.swing.table.*;
  305. import java.awt.event.*;
  306. import java.awt.*;
  307. public class Prg5 extends JFrame
  308. {
  309. JLabel lno;
  310. JTextField tno;
  311. JButton search;
  312. JTable table;
  313. DefaultTableModel model;
  314. public Prg5()
  315. {
  316. String[][] data={};
  317. String[] fields = {"No" , "Name"};
  318. JPanel p1 = new JPanel(new FlowLayout());
  319. lno = new JLabel("Enter No :");
  320. tno = new JTextField(10);
  321. search = new JButton("Search");
  322. model = new DefaultTableModel(data , fields);
  323. table = new JTable(model);
  324. p1.add(lno);
  325. p1.add(tno);
  326. p1.add(search);
  327. JScrollPane pane = new JScrollPane(table);
  328. getContentPane().add(p1);
  329. search.addActionListener(new ActionListener()
  330. {
  331. public void actionPerformed(ActionEvent e)
  332. {
  333. try
  334. {
  335. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  336. Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
  337. Statement st = con.createStatement();
  338. ResultSet rs = st.executeQuery("select * from table1 where table1.no="+tno.getText());
  339. while(rs.next())
  340. {
  341. Object[] obj = {rs.getString(1),rs.getString(2)};
  342. model.addRow(obj);
  343. }
  344. }
  345. catch(Exception p)
  346. {
  347. }
  348. }
  349. });
  350. }
  351. public static void main(String args[])
  352. {
  353. Prg5 jt = new Prg5();
  354. jt.setLayout(new GridLayout(2,1) );
  355. //jt.pack();
  356. jt.setSize(300, 300);
  357. jt.setVisible(true);
  358. }
  359. }
  360. /* This program is done using ACCESS insted of ORACLE...is u want to do it with ORACLE then try with ur self..*/
  361. Program 6
  362. /*
  363. 6. Write a JDBC program to use ResultSetMetaData
  364. */
  365.  
  366. import java.sql.*;
  367.  
  368. public class Prg6
  369. {
  370. public static void main(String args[])
  371. {
  372. try
  373. {
  374. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  375. Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
  376. Statement st = con.createStatement();
  377. ResultSet rs = st.executeQuery("select * from table1");
  378. ResultSetMetaData rsm = rs.getMetaData();
  379.  
  380. System.out.println("Name of Table :" + rsm.getTableName(1));
  381. System.out.println("Number of columns in table :" + rsm.getColumnCount());
  382. System.out.println("First Column Name :" + rsm.getColumnName(1));
  383. System.out.println("First COlumn Type :" + rsm.getColumnTypeName(1));
  384. System.out.println("Second Column Name :"+ rsm.getColumnName(2));
  385. System.out.println("Second COlumn Type :" + rsm.getColumnTypeName(2));
  386. System.out.println("No. Column have Auto Increment :" + rsm.isAutoIncrement(1));
  387.  
  388.  
  389. con.close();
  390. }
  391. catch(Exception e)
  392. {
  393. }
  394. }
  395.  
  396. } program 7
  397. /*
  398. * 7. Write a JDBC program to use DatabaseMetaData
  399. */
  400. import java.sql.*;
  401.  
  402. public class Prg7
  403. {
  404. public static void main(String args[])
  405. {
  406. try
  407. {
  408. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  409. Connection con = DriverManager.getConnection("jdbc:odbc:assign3");
  410.  
  411. DatabaseMetaData dmd = con.getMetaData();
  412.  
  413. System.out.println("DatabaseProductName :" + dmd.getDatabaseProductName());
  414. System.out.println("DatabaseProductVersion :"+ dmd.getDatabaseProductVersion());
  415. System.out.println("DriverName :" + dmd.getDriverName());
  416. System.out.println("DriverVersion :" + dmd.getDriverVersion());
  417. //System.out.println("CatalogSeparator :"+ dmd.getCatalogSeparator());
  418. //System.out.println("SQLKeywords :"+ dmd.getSQLKeywords());
  419.  
  420. }
  421. catch(Exception e)
  422. {
  423. System.out.println(e);
  424. }
  425.  
  426. }
  427.  
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement