parent
6fd5fe8d0a
commit
bea60a3774
@ -0,0 +1,17 @@ |
||||
package me.lensfrex.manager.data; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Map; |
||||
|
||||
public class ManagerTool { |
||||
private final ArrayList<Student> studentData; |
||||
|
||||
public ManagerTool(ArrayList<Student> studentData) { |
||||
this.studentData = studentData; |
||||
} |
||||
|
||||
public void addStudent(long id, String name, Map<String, Integer> score) { |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@ |
||||
package me.lensfrex.manager.data; |
||||
|
||||
import java.util.Map; |
||||
|
||||
public class Student { |
||||
private String name; |
||||
//private String id;
|
||||
private long id; |
||||
private Map<String, Integer> score; |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public long getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(long id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public Map<String, Integer> getScore() { |
||||
return score; |
||||
} |
||||
|
||||
public void setScore(Map<String, Integer> score) { |
||||
this.score = score; |
||||
} |
||||
|
||||
public Student(String name, long id, Map<String, Integer> score) { |
||||
this.name = name; |
||||
this.id = id; |
||||
this.score = score; |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
package me.lensfrex.manager.data; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
public class StudentManager { |
||||
private ArrayList<Student> studentData = new ArrayList<>(); |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,8 @@ |
||||
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"; |
||||
} |
@ -0,0 +1,99 @@ |
||||
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,4 @@ |
||||
- Please enter the student information following this format: |
||||
[id] [name] ...[subjectName,score] |
||||
- Example: |
||||
23333333 Stud English,100 Math,99 History,98 |
Loading…
Reference in new issue