Guest User

Untitled

a guest
Apr 10th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. package C;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9.  
  10. public class jdbc2 {
  11. static void getconnection()
  12. {
  13. try{
  14. Class.forName("oracle.jdbc.driver.OracleDriver");
  15. Connection conn=DriverManager.getConnection
  16. ("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  17. Statement st=conn.createStatement();
  18. conn.setAutoCommit(true);
  19. String sql="CREATE TABLE REGISTRATION" + "(id INTEGER," + " first VARCHAR(10)," + " last VARCHAR(10),"+" age INTEGER)";
  20. //st.executeUpdate(sql);
  21. PreparedStatement ps=conn.prepareStatement("insert into registration values(?,?,?,?)");
  22. for(int i=0;i<3;i++)
  23. {
  24. System.out.println("enter table data id,firstname,lastname,age");
  25. Scanner s1=new Scanner(System.in);
  26. int id=s1.nextInt();
  27. String first=s1.next();
  28. String last=s1.next();
  29. int age=s1.nextInt();
  30. ps.setInt(1,id);
  31. ps.setString(2, first);
  32. ps.setString(3, last);
  33. ps.setInt(4,age);
  34. ps.executeUpdate();
  35. }
  36. ResultSet rs=st.executeQuery("select * from registration");
  37. while(rs.next())
  38. {
  39. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getInt(4));
  40. }
  41. }
  42. catch (Exception e) {
  43. System.out.println("hello");
  44. e.printStackTrace();
  45.  
  46. }
  47.  
  48.  
  49.  
  50. }
  51. public static void main(String[] args) {
  52. getconnection();
  53. }
  54. }
Add Comment
Please, Sign In to add comment