Advertisement
Guest User

JDBC

a guest
Oct 10th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. package oracle;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Oracle {
  6.  
  7.     public static void main(String[] args) {
  8.         Connection conn = null;
  9.         try {
  10.             String driverName = "oracle.jdbc.driver.OracleDriver";
  11.             Class.forName(driverName);
  12.             String serverName = "localhost";
  13.             String serverPort = "1521";
  14.             String sid = "Jami20";
  15.             String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid;
  16.             String username = "JAMI";
  17.             String password = "JAMI20";
  18.             conn = DriverManager.getConnection(url, username, password);
  19.             System.out.println("Connected to the database!");
  20.         } catch (ClassNotFoundException e) {
  21.             System.out.println("Could not find the database driver " + e.getMessage());
  22.         } catch (SQLException e) {
  23.             System.out.println("Could not connect to the database " + e.getMessage());
  24.         }
  25.  
  26.         try {
  27.             Statement st = conn.createStatement();
  28.             ResultSet rs = st.executeQuery("Select * from testing");
  29.             while (rs.next()) {
  30.                 String name = rs.getString("name");
  31.                 int no = rs.getInt("no");
  32.                 System.out.println(no + ": " + name);
  33.             }
  34.             rs.close();
  35.         } catch (Exception e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement