81041
2019-11-12 107623064b162aef3e5d1d734d0763d068a7c4f9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.sqlite_DaoHelper;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 
/**
 * sqlite帮助类,直接创建该类示例,并调用相应的借口即可对sqlite数据库进行操作
 *
 * 本类基于 sqlite jdbc v56
 *
 * @author haoqipeng
 */
public class SqliteHelper {
    private static Connection connection;
    private static Statement statement;
    private static ResultSet resultSet;
    private static String dbFilePath;
 
 
     // 构造函数
     //* dbFilePath sqlite db 文件路径
     // ClassNotFoundException
     //@throws SQLException
 
   /* public SqliteHelper(String dbFilePath) throws ClassNotFoundException, SQLException {
        this.dbFilePath = dbFilePath;
        connection = getConnection(dbFilePath);
    }*/
    public static  void getSqliteHelper() throws ClassNotFoundException, SQLException{
 
        String path=System.getProperty("user.dir");
       // System.out.println(path);
        dbFilePath = path+"/app/src/main/java/com/sqlite_DaoHelper/fbs9600.db";
        //System.out.println(dbFilePath);
        connection = SqliteHelper.getConnection(dbFilePath);
       // System.out.println(connection);
    }
    /**
     * 获取数据库连接
     * @param dbFilePath db文件路径
     * @return 数据库连接
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection(String dbFilePath) throws ClassNotFoundException, SQLException {
        Connection conn = null;
        Class.forName("org.sqlite.JDBC");
        conn = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
        return conn;
    }
 
    /**
     * 执行sql查询
    */
    public static List executeQuery(String sql,Connection conn,Object[] values,CallBack call) throws SQLException, ClassNotFoundException {
        //DBUtil.getConnections();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setQueryTimeout(600);
            //当查询条件不为空时,设置查询条件对应的值
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            rs=ps.executeQuery();
            return call.getResults(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            destroyed();
            //DBUtil.close(rs, ps, conn);
            //System.out.println("释放之后++++++++++++");
            //DBUtil.getConnections();
        }
        return null;
    }
 
    /**
     * 执行select查询,返回结果列表
     *
     * @param sql sql select 语句
     * @param rm 结果集的行数据处理类对象
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    /*public <T> List<T> executeQuery(String sql, RowMapper<T> rm) throws SQLException, ClassNotFoundException {
        List<T> rsList = new ArrayList<T>();
        try {
            resultSet = getStatement().executeQuery(sql);
            while (resultSet.next()) {
                rsList.add(rm.mapRow(resultSet, resultSet.getRow()));
            }
        } finally {
            destroyed();
        }
        return rsList;
    }
*/
    /**
     * 执行数据库更新sql语句
     * @param sql
     * @return 更新行数
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int executeUpdate(String sql) throws SQLException, ClassNotFoundException {
        try {
            int c = getStatement().executeUpdate(sql);
            return c;
        } finally {
            destroyed();
        }
 
    }
 
    /**
     * 执行多个sql更新语句
     * @param sqls
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void executeUpdate(String...sqls) throws SQLException, ClassNotFoundException {
        try {
            for (String sql : sqls) {
                getStatement().executeUpdate(sql);
            }
        } finally {
            destroyed();
        }
    }
 
    /**
     * 执行数据库更新 sql List
     * @param sqls sql列表
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void executeUpdate(List<String> sqls) throws SQLException, ClassNotFoundException {
        try {
            for (String sql : sqls) {
                getStatement().executeUpdate(sql);
            }
        } finally {
            destroyed();
        }
    }
 
    private Connection getConnection() throws ClassNotFoundException, SQLException {
        if (null == connection) connection = getConnection(dbFilePath);
        return connection;
    }
 
    private Statement getStatement() throws SQLException, ClassNotFoundException {
        if (null == statement) statement = getConnection().createStatement();
        return statement;
    }
 
    /**
     * 数据库资源关闭和释放
     */
    public static void destroyed() {
        try {
            if (null != connection) {
                connection.close();
                connection = null;
            }
 
            if (null != statement) {
                statement.close();
                statement = null;
            }
 
            if (null != resultSet) {
                resultSet.close();
                resultSet = null;
            }
        } catch (SQLException e) {
 
        }
    }
 
    public static void main(String[] args) throws ClassNotFoundException, SQLException{
       //SqliteHelper.getSqliteHelper();
       String sql="SELECT * from  tb_battinf ";
        getSqliteHelper();
        List list=SqliteHelper.executeQuery(sql, connection, null, new CallBack() {
            @Override
            public List getResults(ResultSet rs) {
                List list=new ArrayList();
                try {
                    while(rs.next()){
                        list.add(rs.getString("StationId"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return list;
            }
        });
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}