Skip to content

Commit 73b3526

Browse files
Added file reading and data creation and insertion in sqlite.
1 parent 54f1f71 commit 73b3526

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.database;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class FileUltils {
8+
public static String loadTextFile(final String filename) throws IOException{
9+
long time = System.currentTimeMillis();
10+
BufferedReader br = new BufferedReader(new FileReader(filename));
11+
StringBuilder sb = new StringBuilder();
12+
String line;
13+
14+
while((line = br.readLine()) != null){
15+
sb.append(line);
16+
sb.append("\n");
17+
}
18+
br.close();
19+
time = System.currentTimeMillis() - time;
20+
21+
System.out.println("Read file: " + filename + " in " + time + "ms");
22+
return sb.toString();
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.database;
2+
3+
public class Person {
4+
private int id;
5+
private String name;
6+
7+
public int getId() {
8+
return id;
9+
}
10+
11+
public void setId(int id) {
12+
this.id = id;
13+
}
14+
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public Person() {
25+
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.database;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import java.sql.Statement;
6+
7+
public class PersonDao {
8+
private Connection connetion;
9+
public PersonDao() {
10+
connetion = Database.getInstance().getConnection();
11+
12+
}
13+
14+
public void insert(Person person) throws SQLException{
15+
Statement statement = connetion.createStatement();
16+
17+
statement.executeUpdate("insert into person values(" + person.getId() + ", '" + person.getName() + "')");
18+
19+
statement.close();
20+
}
21+
}

demo/src/main/java/com/example/database/TestDB.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package com.example.database;
22

3+
import java.io.IOException;
34
import java.sql.Connection;
45
import java.sql.ResultSet;
56
import java.sql.SQLException;
67
import java.sql.Statement;
78

89
public class TestDB {
9-
public static void main(String[] args) {
10+
public static void main(String[] args) throws IOException{
1011

1112
Connection connection = null;
1213
try {
1314
connection = Database.getInstance().getConnection();
1415
Statement statement = connection.createStatement(); // set timeout to 30 sec.
1516

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+
2033
ResultSet rs = statement.executeQuery("select * from person");
2134
while (rs.next()) {
2235
// read the result set
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
drop table if exists person;
2+
create table person (id integer, name string);
3+
insert into person values(1, 'leo');
4+
insert into person values(2, 'yui')
5+
6+
7+
/* CREATE TABLE IF NOT EXISTS person (
8+
id INTEGER,
9+
name STRING
10+
);
11+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
drop table if exists person;
2+
create table person (id integer, name string);
3+
insert into person values(1, 'leo');
4+
insert into person values(2, 'yui')
5+
6+
7+
/* CREATE TABLE IF NOT EXISTS person (
8+
id INTEGER,
9+
name STRING
10+
);
11+
*/

0 commit comments

Comments
 (0)