· 9 years ago · Jan 11, 2017, 06:12 PM
1import java.sql.Connection;
2import java.sql.Date;
3import java.sql.DriverManager;
4import java.sql.PreparedStatement;
5import java.sql.SQLException;
6import java.sql.Statement;
7import java.time.LocalDate;
8import java.util.Scanner;
9
10public class Main {
11
12 public static void main(String[] args) {
13
14 Scanner input = new Scanner(System.in);
15 Connection conn = null;
16 LocalDate date;
17 Statement stmt = null;
18 PreparedStatement prepStmt = null;
19
20 try {
21 Class.forName("org.sqlite.JDBC");
22 conn = DriverManager.getConnection("jdbc:sqlite:Test.db");
23 stmt = conn.createStatement();
24 System.out.println("Connected to Database");
25 } catch (ClassNotFoundException e) {
26 System.err.println("No JDBC driver");
27 } catch (SQLException e) {
28 System.err.println("Error during connecting");
29 }
30
31 String createTest = "CREATE TABLE IF NOT EXISTS test ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'text' TEXT, 'date' Date)";
32 try {
33 stmt.execute(createTest);
34 } catch (SQLException e) {
35 System.err.println("Error during creating table");
36 }
37
38 date = LocalDate.of(1992, 1, 1);
39 Date dat = Date.valueOf(date);
40 System.out.println(dat);
41
42 String query = "insert into test values(NULL, ?, ?)";
43 System.out.println("Type text");
44 String text = input.nextLine();
45
46 try {
47 prepStmt = conn.prepareStatement(query);
48 prepStmt.setString(1, text);
49 prepStmt.setDate(2, dat);
50 prepStmt.execute();
51 } catch (SQLException e) {
52 System.err.println("Error");
53 }
54
55 input.close();
56 }
57
58}