· 9 years ago · Sep 29, 2016, 12:52 AM
1package dbclasstest;
2
3// Import all of the Java SQL classes
4import java.sql.Connection;
5import java.sql.DriverManager;
6import java.sql.DatabaseMetaData;
7import java.sql.SQLException;
8import java.sql.Statement;
9import java.sql.ResultSet;
10
11/**
12 * Allows easy creation and modification of an SQLite databasn by offering
13 * simple command and methods that can be called.
14 *
15 * @author Dan
16 * @version %I%, %G%
17 * @since 1.0
18 */
19public class db
20{
21 private Connection con;
22 private Statement stmt;
23 private String dburl;
24
25 /**
26 * Creates a .db database file with the name that is passed in 'dbname'
27 *
28 * @param dbname The string name of the database file, e.g. MyDatabase(.db)
29 */
30 public db ( String dbname )
31 {
32 try {
33 Class.forName( "org.sqlite.JDBC" );
34 this.dburl = "jdbc:sqlite:" + dbname + ".db";
35
36 this.con = DriverManager.getConnection( this.dburl );
37 this.stmt = this.con.createStatement();
38 this.notify( "Connection established." );
39 } catch ( SQLException err ) {
40 err.printStackTrace();
41 } catch ( ClassNotFoundException err ) {
42 err.printStackTrace();
43 }
44 }
45
46 /**
47 * Outputs the current driver's metadata.
48 */
49 public void getInfo ()
50 {
51 try {
52 DatabaseMetaData dm = (DatabaseMetaData) this.con.getMetaData();
53
54 System.out.println( "Driver name: " + dm.getDriverName() );
55 System.out.println( "Driver version: " + dm.getDriverVersion() );
56 System.out.println( "Product name: " + dm.getDatabaseProductName() );
57 System.out.println( "Product version: " + dm.getDatabaseProductVersion() );
58 } catch ( SQLException err ) {
59 err.printStackTrace();
60 }
61 }
62
63 /**
64 * Creates a table in the currently opened database.
65 *
66 * @param table_name Name of the table to be created.
67 * @param columns An array of columns to be added to the table, the columns should be in order.Data types
68 * must also be defined in the string, e.g. "Names VARCHAR(60)".
69 */
70 public void createTable( String table_name, String[] columns )
71 {
72 String query = "CREATE TABLE IF NOT EXISTS " + table_name + " (";
73
74 for ( int x = 0; x < columns.length; x++ )
75 {
76 if ( x+1 == columns.length )
77 {
78 query = query + columns[x] + ")";
79 }
80 else
81 {
82 query = query + columns[x] + ", ";
83 }
84 }
85
86 try { this.stmt.executeUpdate( query ); } catch ( SQLException err ) { err.printStackTrace(); }
87 }
88
89 public void insert ( String table, String[][] elements )
90 {
91
92 }
93
94 public void update ( String query ) // TODO: allow custom update definitions
95 {
96 try { this.stmt.executeUpdate( query ); } catch ( SQLException err ) { err.printStackTrace(); }
97 }
98
99 public void query ( String query ) // TODO: allow custom query definitions
100 {
101 try {
102 ResultSet rs = this.stmt.executeQuery( query );
103
104 while ( rs.next() )
105 {
106 System.out.print( rs.getInt( "ID" ) + " " );
107 System.out.print( rs.getString( "FirstName" ) + " " );
108 System.out.print( rs.getString( "LastName" ) + "\n" );
109 }
110 } catch ( SQLException err ) { err.printStackTrace(); }
111 }
112
113 private boolean CheckPrimaryKeyIsValid ( String table, int key )
114 {
115 try {
116 ResultSet rs = this.stmt.executeQuery( "SELECT ID FROM " + table );
117
118 while ( rs.next() )
119 {
120 if ( rs.getInt( "ID" ) == key ) {
121 this.notify( "Key already exists in database." );
122 return false;
123 } else return true;
124 }
125 } catch ( SQLException err ) { err.printStackTrace(); }
126 }
127
128 private void notify ( String message ) { System.out.println( "[SQLite]: " + message ); }
129}