Guest User

Untitled

a guest
Dec 11th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class DatabaseManagement
  5. {
  6.     Connection con;
  7.     Statement statement; //used for sending commands to DB
  8.     ResultSet rs; //for results from database
  9.    
  10.     /**
  11.      * constructor opens up the database
  12.      */
  13.     public DatabaseManagement()
  14.     {
  15.         try
  16.         {
  17.            
  18.             String dataSourceName = "AccessDB";
  19.            
  20.             String dbURL = "jdbc:odbc:" + dataSourceName;
  21.            
  22.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  23.             // set this to a MS Access DB you have on your machine
  24.             String filename = "C:/Database/StockData.accdb";
  25.            
  26.             String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
  27.            
  28.             database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
  29.            
  30.             // now we can get the connection from the DriverManager
  31.             con = DriverManager.getConnection( dbURL ,"","");
  32.             /*
  33.             statement = con.createStatement();
  34.            
  35.             create();
  36.             */
  37.            
  38.         }
  39.         catch (Exception e)
  40.         {
  41.             System.out.println("Error: " + e);
  42.         }
  43.     }
  44.  
  45.  
  46.     //creates the table Stocks in the database if its never been created before
  47.     public void create()
  48.     {
  49.         try
  50.         {
  51.             statement.execute("create table Stocks(ticker_symbol text,stock_date date,open_price double,high_price double,low_price double,close_price double,volume long)");
  52.         }
  53.         catch(SQLException e){}
  54.     }
Add Comment
Please, Sign In to add comment