Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPasswordField;
  10. import javax.swing.JTextField;
  11. import oracle.jdbc.pool.OracleDataSource;
  12.  
  13. public class JDBCExample {
  14. String jdbcUrl = "jdbc:oracle:thin:@akka.d.umn.edu:1521:xe";
  15. Connection conn;
  16.  
  17. public Connection getDBConnection() throws SQLException{
  18. OracleDataSource ds = new OracleDataSource();
  19.  
  20.  
  21. ds.setURL(jdbcUrl);if(conn == null) {
  22. // Display a message to get the password from the user
  23. JLabel label = new JLabel("Oracle Username: ");
  24. JTextField jtf = new JTextField();
  25. JLabel label2 = new JLabel("Oracle Password:");
  26. JPasswordField jpf = new JPasswordField();
  27. JOptionPane.showConfirmDialog(null,new Object[]{label, jtf, label2, jpf}, "Password:",
  28. JOptionPane.OK_CANCEL_OPTION);
  29. String password = String.valueOf(jpf.getPassword());
  30. conn = ds.getConnection(jtf.getText(), password );
  31. }
  32.  
  33. conn.setAutoCommit(true);
  34. return conn;
  35. }
  36.  
  37. public static void main(String[] args) throws Exception{
  38. String ticker;
  39. int choice;
  40. //boolean answer;
  41.  
  42. JDBCExample a = new JDBCExample();
  43. Connection conn = a.getDBConnection();
  44.  
  45. Scanner input = new Scanner(System.in);
  46.  
  47. System.out.println("Either 'ticker_symbol' (1), 'avg_bal' (2), or quit(0).\n");
  48. choice = input.nextInt();
  49. switch(choice)
  50. {
  51. case 1 :
  52. System.out.println("Enter the ticker symbol\n");
  53. ticker = input.next();
  54. a.ticker_symbol(ticker,conn);
  55. break;
  56.  
  57. case 2 :
  58. a.avg_bal(conn);
  59. break;
  60.  
  61. default :
  62. return;
  63. }
  64. }
  65.  
  66. public void ticker_symbol (String ticker, Connection conn) throws SQLException{
  67.  
  68. String selectSQL = "select client_ssn, name, email from s_client where s_client.client_ssn = (select client_ssn from transaction where transaction.cusip = (select cusip from stock where ticker = (?)))";
  69.  
  70. PreparedStatement preparedStatement = conn.prepareStatement(selectSQL);
  71. preparedStatement.setString(1, ticker);
  72. ResultSet rs = preparedStatement.executeQuery(selectSQL);
  73. while (rs.next()) {
  74. int ssn = rs.getInt("client_ssn");
  75. System.out.println(ssn);
  76. String name = rs.getString("name");
  77. System.out.println(name);
  78. String email = rs.getString("email");
  79. System.out.println(email);
  80.  
  81. }
  82.  
  83. }
  84.  
  85. public void avg_bal(Connection conn) throws SQLException{
  86. String selectSQL = "select s_client.state, avg(account.balance) from account, s_client group by s_client.state;";
  87. Statement stmt = conn.createStatement();
  88.  
  89. ResultSet rs = stmt.executeQuery(selectSQL);
  90. while(rs.next()){
  91. String state = rs.getString(1);
  92. System.out.println(state);
  93. int bal = rs.getInt("avg(account.balance)");
  94. System.out.println(bal);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement