Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercises;
- import java.io.*;
- import java.sql.*;
- /*
- * @writer Jason Goldenberg
- * Created March 29th, 2017
- * Purpose: connect to database using Oracle
- */
- public class jdbcEx1 {
- static Connection conn = null;
- static Statement stmt = null;
- static String dbURL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
- static String user = "projects";
- static String password = "Password";
- public jdbcEx1() { }
- public static void doQ1(Connection conn) throws SQLException
- {
- try {
- conn = DriverManager.getConnection(dbURL, user, password);
- System.out.println("Connection opened! For driver ==> Oracle 11XE");
- stmt = conn.createStatement();
- ResultSet qw = stmt.executeQuery("select productid, productname, unitprice" + " from VQ1");
- System.out.println("lab 10 ~ Jason Goldenberg,");
- System.out.printf("%11s %11s %11s\n", "I.D", "product", "Prece");
- System.out.printf("%11s %11s %11s\n", "===", "===", "===");
- while (qw.next()) {
- System.out.println(qw.getString(1) + "\t" + qw.getString(2) + "\t\tqqqqq\t\t$" + qw.getString(3));
- }
- qw.close();
- } catch (SQLException m) {
- System.err.println(m.getMessage());
- } finally {
- if (!conn.isClosed()) {
- conn.close();
- System.out.println("the connection is now closed");
- }
- }
- }
- public static void doQ2(Connection conn) throws SQLException
- {
- try {
- conn = DriverManager.getConnection(dbURL, user, password);
- conn.clearWarnings();
- System.out.println("Connection opened! For driver ==> Oracle 11XE");
- stmt = conn.createStatement();
- ResultSet variable = stmt.executeQuery("alter session set nls_date_format = 'DD-MM-YYYY'");
- ResultSet rs = stmt.executeQuery("select * from VQ2");
- System.out.println("");
- System.out.println("Lab 10 ~ Jason Goldenberg");
- System.out.printf("%10s %12s %23s %25s\n", "Shipped Date", "Order Date", "ORDERID", "Company Name");
- System.out.printf("%10s %12s %23s %25s\n", "=====", "======", "========", "========");
- while (rs.next()) {
- System.out.println(rs.getString("OrderID") + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString("companyname"));
- }
- rs.close();
- variable.close();
- } catch (SQLException m) {
- System.err.println(m.getMessage());
- } finally {
- if (!conn.isClosed()) {
- conn.close();
- System.out.println("Connection Closed! Oracle");
- }
- }
- }
- public static void main (String args[]) throws SQLException, IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String option = "";
- while (!option.equalsIgnoreCase("X")) {
- System.out.print("Please choose 'Q1' or 'Q2?' or 'X' to end: ");
- option = br.readLine();
- System.out.println("");
- if (option.equalsIgnoreCase("1")){
- doQ1(conn);
- }
- if (option.equalsIgnoreCase("2")){
- doQ2(conn);
- System.out.println("");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement