OSDN Git Service

modified: build/classes/parauni/CommandController.class
[parauni/para-repos.git] / src / parauni / DataBase.java
1 package parauni;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10 import java.util.regex.Pattern;
11
12 /**
13  *
14  * @author karihei
15  */
16 public class DataBase {
17     String TASK_TBL = "task_tbl";
18         String VM_TBL = "vm_tbl";
19     Connection con;
20     Statement st;
21
22     public DataBase(){
23         try {
24             Class.forName("org.sqlite.JDBC");
25                         try {
26                                 con = DriverManager.getConnection("jdbc:sqlite:puc.db");
27                     st = con.createStatement();
28                         st.setQueryTimeout(1);
29                             con.setAutoCommit(false);
30                         } catch (SQLException ex) {
31                                 Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
32                         }          
33         } catch (ClassNotFoundException ex) {
34             Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
35         }
36     }
37
38     
39     // データベースを初期化
40     public void initDB() throws SQLException{
41         con = DriverManager.getConnection("jdbc:sqlite:puc.db");
42         st = con.createStatement();
43         st.executeUpdate("create table task_tbl (" +
44                 "TASKID int," +
45                 "TASKNAME string," +
46                 "VMID int," +
47                 "SDATE datatime," +
48                 "EDATE datatime," +
49                 "OUTLINE string)");
50     }
51
52     public void commit() throws SQLException{
53         con.commit();
54     }
55
56     public ResultSet executeQuery(String sql){
57       ResultSet rs;
58       try {
59         rs = st.executeQuery(sql);
60         return rs;
61       } catch (SQLException ex) {
62         Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
63       }
64       return null;
65     }
66             
67
68     public int executeUpdate(String sql){
69       try {
70         return st.executeUpdate(sql);
71       } catch (SQLException ex) {
72         Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
73       }
74       return -1;
75     }
76
77     public void close(){
78       try {
79         con.close();
80         st.close();
81       } catch (SQLException ex) {
82         Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
83       }  
84     }
85
86     public boolean isDate(String date){
87         if(Pattern.matches("\\d{4}-\\d{2}-\\d{2}", date)){
88             return true;
89         } else {
90             return false;
91         }
92
93     }
94
95     public Connection getConnection(){
96         return con;
97     }
98
99     public Statement getStatment(){
100         return st;
101     }
102 }