|
1 | 1 | package com.example.database;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.sql.Connection;
|
4 | 5 | import java.sql.ResultSet;
|
5 | 6 | import java.sql.SQLException;
|
6 | 7 | import java.sql.Statement;
|
7 | 8 |
|
8 | 9 | public class TestDB {
|
9 |
| - public static void main(String[] args) { |
| 10 | + public static void main(String[] args) throws IOException{ |
10 | 11 |
|
11 | 12 | Connection connection = null;
|
12 | 13 | try {
|
13 | 14 | connection = Database.getInstance().getConnection();
|
14 | 15 | Statement statement = connection.createStatement(); // set timeout to 30 sec.
|
15 | 16 |
|
16 |
| - statement.executeUpdate("drop table if exists person"); |
17 |
| - statement.executeUpdate("create table person (id integer, name string)"); |
18 |
| - statement.executeUpdate("insert into person values(1, 'leo')"); |
19 |
| - statement.executeUpdate("insert into person values(2, 'yui')"); |
| 17 | + // create a database connection |
| 18 | + String sql = FileUltils.loadTextFile("demo/src/main/java/com/example/resource/description.sql"); |
| 19 | + /* System.out.println(sql); */ |
| 20 | + |
| 21 | + // Run the query |
| 22 | + statement.executeUpdate(sql); |
| 23 | + |
| 24 | + // Insert data |
| 25 | + Person person = new Person(); |
| 26 | + person.setId(3); |
| 27 | + person.setName("Gean"); |
| 28 | + |
| 29 | + // data access object |
| 30 | + PersonDao personDao = new PersonDao(); |
| 31 | + personDao.insert(person); |
| 32 | + |
20 | 33 | ResultSet rs = statement.executeQuery("select * from person");
|
21 | 34 | while (rs.next()) {
|
22 | 35 | // read the result set
|
|
0 commit comments