forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeDBFile.java
More file actions
67 lines (60 loc) · 2.19 KB
/
MakeDBFile.java
File metadata and controls
67 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Java. Level 2. Lesson 7
* Making SQLite db file with users
* Note:
* a) Download latest ver of sqlite-jdbc-(VER).jar from
* https://bitbucket.org/xerial/sqlite-jdbc/downloads
* b) Put this jar into [JDK]\jre\lib\ext
* c) See also http://www.tutorialspoint.com/sqlite/sqlite_java.htm
*
* @author Sergey Iryupin
* @version 0.3 dated Jan 16, 2018
*/
import java.sql.*;
class MakeDBFile implements IConstants {
final String NAME_TABLE = "users";
final String SQL_CREATE_TABLE =
"DROP TABLE IF EXISTS " + NAME_TABLE + ";" +
"CREATE TABLE " + NAME_TABLE +
"(login CHAR(6) PRIMARY KEY NOT NULL," +
" passwd CHAR(6) NOT NULL);";
final String SQL_INSERT_MIKE =
"INSERT INTO " + NAME_TABLE +
" (login, passwd) " +
"VALUES ('mike', 'qwe');";
final String SQL_INSERT_JONH =
"INSERT INTO " + NAME_TABLE +
" (login, passwd) " +
"VALUES ('john', 'rty');";
final String SQL_SELECT = "SELECT * FROM " + NAME_TABLE + ";";
public static void main(String[] args) {
new MakeDBFile();
}
MakeDBFile() {
try {
// loads a class, including running its static initializers
Class.forName(DRIVER_NAME);
// attempts to establish a connection to the given database URL
Connection connect = DriverManager.getConnection(SQLITE_DB);
// сreates an object for sending SQL statements to the database
Statement stmt = connect.createStatement();
// create table
stmt.executeUpdate(SQL_CREATE_TABLE);
// insert record(s)
stmt.executeUpdate(SQL_INSERT_MIKE);
stmt.executeUpdate(SQL_INSERT_JONH);
// print records
ResultSet rs = stmt.executeQuery(SQL_SELECT);
System.out.println("LOGIN\tPASSWD");
while (rs.next())
System.out.println(
rs.getString("login") + "\t" +
rs.getString(PASSWD_COL));
rs.close();
stmt.close();
connect.close();
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
}