Advertisement
vvccs

DB1

Apr 26th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Database {
  4. public static void main(String[] args) {
  5.  
  6. try {
  7. // step1 load the driver class
  8. Class.forName("oracle.jdbc.driver.OracleDriver");
  9.  
  10. // step2 create the connection object
  11. Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "system");
  12.  
  13. // step3 create the statement object
  14. Statement stmt = con.createStatement();
  15.  
  16. // For inserting Data
  17. // ----------------------
  18. // Statement insertQuery = con.createStatement();
  19. // insertQuery.executeQuery("INSERT INTO emp (ID, Name, Age) VALUES (3, 'Vikas', 23)");
  20. // ----------------------
  21.  
  22. // step4 execute query
  23. ResultSet rs;
  24. rs = stmt.executeQuery("SELECT * FROM emp");
  25.  
  26. while (rs.next()) {
  27. System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getInt(3));
  28. }
  29.  
  30. // step5 close the connection object
  31. con.close();
  32.  
  33. }
  34. catch (Exception e) {
  35. System.out.println(e);
  36. }
  37.  
  38. }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement