Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 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. int ticker, choice;
  39.  
  40. JDBCExample a = new JDBCExample();
  41. Connection conn = a.getDBConnection();
  42.  
  43. Scanner input = new Scanner(System.in);
  44.  
  45. System.out.println("Either 'purchase_stock_with_ticker' (1), 'avg_state_account_bal' (2), or quit(0).\n");
  46. choice = input.nextInt();
  47. switch(choice)
  48. {
  49. case 1 :
  50. System.out.println("Enter the ticker symbol\n");
  51. ticker = input.nextInt();
  52. a.purchase_stock_with_ticker(ticker, conn);
  53. break;
  54.  
  55. case 2 :
  56. a.avg_state_account_bal(conn);
  57. break;
  58.  
  59. default :
  60. return;
  61. }
  62. }
  63.  
  64. public void purchase_stock_with_ticker (int ticker,Connection conn) throws SQLException{
  65. //Connection conn = new getDBConnection();
  66. PreparedStatement stmt=conn.prepareStatement("");
  67. stmt.setInt(1,ticker);
  68.  
  69. int i = stmt.executeUpdate();
  70. }
  71.  
  72. public void avg_state_account_bal(Connection conn) throws SQLException{
  73. String avg_acc_bal = "select avg(account.balance) " +
  74. "from account, client " +
  75. "group by client.state;";
  76.  
  77. Statement stmt = conn.createStatement();
  78.  
  79. ResultSet rs = stmt.executeQuery(avg_acc_bal);
  80. while(rs.next()){
  81. int bal = rs.getInt(1);
  82. System.out.println(bal);
  83. }
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement