/Users/lyon/j4p/src/rdbms/DatabaseWriter.java
|
1 package rdbms;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7
8 public class DatabaseWriter {
9 private String url = null; // "jdbc:odbc:udb";
10 private String uid = "";
11 private String pw = "";
12 private String driverName = null; //"sun.jdbc.odbc.JdbcOdbcDriver";
13 private Connection c = null;
14
15 public DatabaseWriter(
16 String _url,
17 String _uid,
18 String _password,
19 String _driverName) throws
20 ClassNotFoundException {
21 url = _url;
22 uid = _uid;
23 pw = _password;
24 driverName = _driverName;
25 Class.forName(driverName);
26 }
27
28 public static boolean batchExecute(
29 String _url,
30 String _uid,
31 String _pw,
32 String _driver,
33 String sql[]) {
34 try {
35 DatabaseWriter dbw = new
36 DatabaseWriter(_url, _uid, _pw, _driver);
37 dbw.execute(sql);
38 return true;
39
40 } catch (Exception e) {
41 System.out.println(e);
42 return false;
43 }
44 }
45
46 public static boolean batchExecute(String sql[]) {
47 try {
48 DatabaseWriter dbw = new
49 DatabaseWriter(
50 "jdbc:odbc:udb", "", "", "sun.jdbc.odbc.JdbcOdbcDriver");
51 dbw.execute(sql);
52
53 } catch (Exception e) {
54 System.out.println(e);
55 return false;
56 }
57 return true;
58 }
59
60 public void execute(String sql[])
61 throws SQLException {
62 c = DriverManager.getConnection(
63 url, uid, pw);
64 Statement s = c.createStatement();
65 for (int i = 0; i < sql.length; i++)
66 s.execute(sql[i]);
67 s.close();
68 c.close();
69
70 }
71 }