tampurus

Java connectivity with sql

Jun 7th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. // hum
  2.  
  3. package ramesh.java;
  4. import java.sql.*;
  5. public class RameshJava {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         try {
  10.             Class.forName("oracle.jdbc.driver.OracleDriver");
  11.             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
  12.             Statement st = con.createStatement();
  13.             st.executeUpdate("insert into ramesh values (5,'Raju')");
  14.            
  15.             con.close();
  16.         }
  17.         catch(Exception E){
  18.             System.out.println("Exception caught "+E);
  19.         }
  20.      }
  21. }
  22.  
  23.  
  24. // java t point
  25. import java.sql.*;  
  26. class OracleCon{  
  27. public static void main(String args[]){  
  28. try{  
  29. //step1 load the driver class  
  30. Class.forName("oracle.jdbc.driver.OracleDriver");  
  31.  
  32. //step2 create  the connection object  
  33. Connection con=DriverManager.getConnection(  
  34. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  35.  
  36. //step3 create the statement object  
  37. Statement stmt=con.createStatement();  
  38.  
  39. //step4 execute query  
  40. ResultSet rs=stmt.executeQuery("select * from emp");  
  41. while(rs.next())  
  42. System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
  43.  
  44. //step5 close the connection object  
  45. con.close();  
  46.  
  47. }catch(Exception e){ System.out.println(e);}  
  48.  
  49. }  
  50. }  
Add Comment
Please, Sign In to add comment