parent
4ccb974d59
commit
222d88d5ad
@ -1,8 +0,0 @@ |
||||
package me.lensfrex.manager.sqlite; |
||||
|
||||
public class DataTypes { |
||||
public static final String SQL_DATA_TYPE_NUMBER = "int"; |
||||
public static final String SQL_DATA_TYPE_TEXT = "text"; |
||||
public static final String SQL_DATA_TYPE_REAL = "real"; |
||||
public static final String SQL_DATA_TYPE_NULL = "null"; |
||||
} |
@ -1,99 +0,0 @@ |
||||
package me.lensfrex.manager.sqlite; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.DriverManager; |
||||
import java.sql.SQLException; |
||||
import java.sql.Statement; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* SQL操作的简单封装... |
||||
* 试图手动封装SQL操作... |
||||
* 有BUG?这就对了 |
||||
* SQL语句仍在学习中... |
||||
* SQL语句拼接怎么这么麻烦... |
||||
*/ |
||||
public class SQLiteTool { |
||||
private static final String DATABASE_LOCATION = "student.db"; |
||||
|
||||
private Connection dbConnection; |
||||
private Statement statement; |
||||
|
||||
/** |
||||
* 初始化数据库文件,若成功则返回true,若数据库初始化出现异常则返回false。 |
||||
* 不保证数据库中存在欲操作的表,需手动确认。 |
||||
* |
||||
* @return 初始化是否成功,若成功则返回true,若数据库初始化出现异常则返回false。 |
||||
*/ |
||||
public boolean initDatabase() { |
||||
try { |
||||
Class.forName("org.sqlite.JDBC"); |
||||
this.dbConnection = DriverManager.getConnection("jdbc:sqlite:" + DATABASE_LOCATION); |
||||
this.statement = dbConnection.createStatement(); |
||||
|
||||
return true; |
||||
} catch (Exception e) { |
||||
System.out.println("Some errors happens when initialing database."); |
||||
e.printStackTrace(); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 向数据库中创建一个表。 |
||||
* |
||||
* @param tableName 数据库表的名称 |
||||
* @param columns 数据库表列的参数,map中key为列的名称,value为其相应的数据类型。 |
||||
* @throws SQLException 如果创建表的过程中发生错误 |
||||
*/ |
||||
public void createTable(String tableName, HashMap<String, String> columns) throws SQLException { |
||||
StringBuilder stringBuilder = new StringBuilder("create table %s ("); |
||||
|
||||
for (Map.Entry<String, String> column : columns.entrySet()) { |
||||
stringBuilder.append(String.format("%s %s, ", column.getKey(), column.getValue())); |
||||
} |
||||
|
||||
String createStatement = stringBuilder |
||||
.replace(stringBuilder.lastIndexOf(","), stringBuilder.lastIndexOf(",") + 2, ")") |
||||
.toString(); |
||||
|
||||
statement.executeUpdate(createStatement); |
||||
} |
||||
|
||||
/** |
||||
* 查询表格是否存在。 |
||||
* |
||||
* @param tableName 表格名 |
||||
* @return 存在,返回true,否则返回false; |
||||
*/ |
||||
public boolean isTableExists(String tableName) { |
||||
String tryStatement = String.format("select * from %s", tableName); |
||||
try { |
||||
statement.executeQuery(tryStatement); |
||||
return true; |
||||
} catch (Exception e) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 向数据库里插入一条记录。 |
||||
* |
||||
* @param table 表格名 |
||||
* @param values 每一列的数据值,文本类型数据应使用单引号 ' 括住 |
||||
* @throws SQLException 如果执行的过程中发生错误 |
||||
*/ |
||||
public void insert(String table, String... values) throws SQLException { |
||||
StringBuilder stringBuilder = new StringBuilder(); |
||||
stringBuilder.append("insert into ").append(table).append(" values ( "); |
||||
for (String value : values) { |
||||
stringBuilder.append(value).append(' '); |
||||
} |
||||
stringBuilder.append(")"); |
||||
|
||||
statement.executeUpdate(stringBuilder.toString()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,3 @@ |
||||
Manifest-Version: 1.0 |
||||
Main-Class: me.lensfrex.manager.Main |
||||
|
@ -1,5 +1,6 @@ |
||||
---------------------------------------------------------------- |
||||
欢迎来到 SManager-S |
||||
源代码:https://git.ciduid.top/lensfrex/SManager-S |
||||
---------------------------------------------------------------- |
||||
您想做什么: |
||||
1. 添加学生 |
@ -1,5 +1,6 @@ |
||||
---------------------------------------------------------------- |
||||
Welcome to SManager-S |
||||
Source code: https://git.ciduid.top/lensfrex/SManager-S |
||||
---------------------------------------------------------------- |
||||
Do you want to: |
||||
1. Input record |
Loading…
Reference in new issue