1、导入sqlite包
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
2、创建db文件
public synchronized static Connection getConnection() throws SQLException {
if (connection == null) {
try {
String driverClass = "org.sqlite.JDBC";
Class.forName(driverClass);
} catch (Exception e) {
e.printStackTrace();
}
connection=DriverManager.getConnection("jdbc:sqlite:lizhi.db");
return connection;
} else {
return connection;
}
}
3、创建表
public static final String DB_USER = "create table login (" +
" id text(50) PRIMARY KEY," +
" username text(11) not null," +
" password text(20)," +
" token text(100)," +
" topict text(40)," +
" organname text(40)," +
" type integer(1)," +
" is_pwd integer(1)," +
" is_login integer(1)" +
");";
public static final String DB_IPPORT = "create table ipport (" +
" id INTEGER PRIMARY KEY," +
" ip text(11) not null," +
" port text(20) not null" +
");";
public static void createDatabases() throws SQLException, IOException {
Connection connection = getConnection();
Statement statement = connection.createStatement();
statement.execute(DB_USER);
statement.execute(DB_IPPORT );
connection.createStatement().executeUpdate("insert into ipport(ip,port) values('127.0.0.1','8087')")
}
4、新增数据
public static void insert(Login login) {
List<Login> collectionBoxData = getCollectionBoxData(login.getUsername(), login.getPassword());
if (collectionBoxData.size()!=0){
login(login);
}else{
try {
getConnection().createStatement().executeUpdate("insert into login(id,username, password, token,topict,organname,type,is_pwd,is_login) " +
"values ('"+login.getId()+"','"+login.getUsername()+"','"+login.getPassword()+"','"+login.getToken()+"','"+login.getOrgCode()
+"','"+login.getOrganname()+"',"+login.getType()+","+login.getIsPwd()+","+login.getIsLogin()+");");
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
5、查询
public static List<Login> getCollectionBoxData(String username,String password) {
List<Login> result = new ArrayList<>();
try {
ResultSet rs = getConnection().createStatement().executeQuery("select * from main.login where username='"+username+"' and password='"+password+"'");
while (rs.next()) {
Login l = new Login();
l.setUsername(rs.getString("username"));
l.setPassword(rs.getString("password"));
l.setToken(rs.getString("token"));
l.setOrgCode(rs.getString("topict"));
l.setType(rs.getInt("type"));
l.setOrganname(rs.getString("organname"));
result.add(l);
}
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
6、修改
public static void updatePheConfig(String ip,String port){
try {
getConnection().createStatement().executeUpdate("update ipport set ip= '"+ip+"',port='"+port+"' where id = 2");
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
7、删除
public static void delete(String id){
try {
getConnection().createStatement().executeUpdate("delete from login where id = '"+id+"'");
} catch (SQLException e) {
e.printStackTrace();
}
}
|