Through JavaSE, we develop and design a library management system on the offline environment. In this system, we can realize the functions of library information management and library staff management. The system is oriented to the staff of the library, and it is required to complete the basic management operation in the system.
sql
: Placing the database filescom.gregperlinli.bean
: Entity class corresponding to database table one by one (the properties of the class correspond to the keys of the table in the database one by one)com.gregperlinli.dao
: Database Acess Object Place database or file crud (create, retrieve, update and delete) codecom.gregperlinli.service
: Place the code to process the transaction logic and isolate the Dao layer from the view layercom.gregperlinli.utils
: Utilities class, place the common code for all projects which has nothing to do with transaction logiccom.gregperlinli.view
: Place interface related codecom.gregperlinli.test
: Using to debug DAO service, if you want to use it, please add the library JUnit
(Minium version is 5.4)User
: Storge the account data of the user (each user corresponding to a common staff or curator)CommonStaff
: Storge the common staff informationCurator
: Storge the curator informationBook
: Storage book informationSchedule
: Storage the scheduling of the comon staff (under construction)
Java
: Required version is 15.0 or higher
MySQL
: Required version is 8.0.22 or higher
Mysql connector java
: MySQL JDBC driver (Minium version is 8.0.16)Druid
: a database connection pool utility (Minium version is 1.2.4)JUnit
: Code debug utility (optionl, Minium version is 5.4).resource/db/LibrarySystem.sql
to import the databaseJDBC configure: .src/com/gregperlinli/prop/jdbc.properties
# get basic information
# login username
user=librarySystemManager
# login password
password=123456
# the url of the database
url=jdbc:mysql://localhost:3306/LibrarySystem?rewriteBatchedStatements=true
# database driver
driverClass=com.mysql.cj.jdbc.Driver
Druid configure: .src/com/gregperlinli/prop/druid.properties
# Provide four basic information to get the connection
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/LibrarySystem?rewriteBatchedStatements=true
username=librarySystemManager
password=123456
# The basic information to manage the database connection pool
initialSize=10
maxActive=10
ISBN
, name
, category
and author
ID
, UID
, name
, account
and gender
morning
, noon
, afternoon
, evening
and night
)./src/com/gregperlinli/dao/BaseDAO.java
/**
* @Description Generic add, delete and update operation (version2.0) (Consider database transaction)
* @author gregperlinli
* @param conn connection of database
* @param sql sql format
* @param args fill the placeholder(variable)
*
* How to output:
* BaseDAO.update(conn, sql, ...args);
*/
public int update(Connection conn, String sql, Object ...args) throws Exception { // the length of variable parameters should be equal to the number of placeholders
PreparedStatement ps = null;
try {
// precompiled Statement and return ps instance
ps = conn.prepareStatement(sql);
// fill placeholder
for ( int i = 0; i < args.length; i++ ) {
// Be careful to the parameter declaration exception
ps.setObject(i+1, args[i]);
}
// perform operation
return ps.executeUpdate();
} catch ( Exception e ) {
e.printStackTrace();
} finally {
// close the resource
JDBCUtills.closeResource(null, ps);
}
return 0;
}
/**
* @Description Generic query with one object
* using PreparedStatement to realize the query operation by different form (version 2.0, consider to transaction)
* @author gregperlinli
* @param conn connection of database
* @param sql sql format
* @param args fill the placeholder(variable)
* @return Class or null
*
* How to output:
* ClassName className = getQuery(conn, ClassName.class, sql, ...args);
*/
public T getQuery(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
// get the metadata of the result set:ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
// get the column of the result set through metadata
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance();
// handle each column in one row of the result set
for (int i = 0; i < columnCount; i++) {
// get the value of the column
Object columnValue = rs.getObject(i + 1);
// get name of each row
String columnName = rsmd.getColumnLabel(i + 1);
// give one corresponding "columnName" of the Object "t" assignment to "columnValue" through reflex
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch ( Exception e ) {
e.printStackTrace();
} finally {
JDBCUtills.closeResource(null, ps, rs);
}
return null;
}
/**
* @Description Generic query with multiple object
* using PreparedStatement to realize multi query operation by different form (version 2.0, consider to transaction)
* @author gregperlinli
* @param conn connection of database
* @param sql sql format
* @param args fill the placeholder(variable)
* @return List or null
*
* How to output:
* List<ClassName> listName = getMultiQuery(Classname.class, sql, ...args);
*
* How to print out:
* listName.forEach(System.out::println);
*/
public List<T> getMultiQuery(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
// get the metadata of the result set:ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
// get the column of the result set through metadata
int columnCount = rsmd.getColumnCount();
// create an aggregate object
ArrayList<T> list = new ArrayList<T>();
while (rs.next()) {
T t = clazz.newInstance();
// handle each column in one row of the result set and assignment the value to object "t"
for (int i = 0; i < columnCount; i++) {
// get the value of the column
Object columnValue = rs.getObject(i + 1);
// get name of each row
String columnName = rsmd.getColumnLabel(i + 1);
// give one corresponding "columnName" of the Object "t" assignment to "columnValue" through reflex
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
return list;
} catch ( Exception e ) {
e.printStackTrace();
} finally {
JDBCUtills.closeResource(null, ps, rs);
}
return null;
}
Note: These function are universal, so you can use it in anywhere
String
text, you need to press enter twice before the program can respond此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。