📄 setupdatabase.java
字号:
import java.sql.*;
import javagently.*;
import java.io.*;
class SetUpDatabase {
/* Database setup program by B Worrall August 2000
* ====================== amended by J M Bishop
*
* Sets up a database of rates data
* as read in. The database uses Sun's
* jdbc:odbc driver to connect to a text based
* Access Database.
*/
private Connection conn;
SetUpDatabase() {
// Load JDBC-ODBC Bridge driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException c) {
System.out.println("Could not load database driver.");
}
// Read the data from the text file and store it in the
// database.
try {
OpenConnection();
WriteData();
} catch (SQLException s) {
System.out.println(s);
} catch (IOException i) {
System.out.println(i);
}
}
void OpenConnection() throws SQLException {
conn = DriverManager.getConnection("jdbc:odbc:Rates");
Statement s = conn.createStatement();
String state = "DROP TABLE rates";
try {
// Delete old data if necessary (if no such data,
// exception is flagged and we carry on)
int temp = s.executeUpdate(state);
} catch (SQLException q) {}
// Create table with columns in database
state = "CREATE TABLE rates (Code VARCHAR(3), Country VARCHAR(80), " +
"Curr VARCHAR(30), Conv DOUBLE)";
int temp = s.executeUpdate(state);
}
void WriteData() throws IOException, SQLException {
Stream fin = new Stream("rates.data", Stream.READ);
try {
for (; ;) {
Rates r = new Rates();
r.setRate(fin);
// add data into rates
Statement s = conn.createStatement();
String state = "INSERT INTO rates VALUES ('" +
r.code + "', '" +
r.country + "', '" +
r.currency + "', " +
r.conversion + ")";
int rs = s.executeUpdate(state);
}
} catch (EOFException e) {}
}
public static void main (String [] args) {
new SetUpDatabase();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -