爱他就要打他
This commit is contained in:
parent
fe9e8b1482
commit
d5c64eb03e
@ -1,7 +1,10 @@
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import bean.Author;
|
||||||
import bean.Book;
|
import bean.Book;
|
||||||
|
import bean.Publisher;
|
||||||
import utils.DbUtil;
|
import utils.DbUtil;
|
||||||
import utils.system;
|
import utils.system;
|
||||||
|
|
||||||
@ -11,6 +14,7 @@ public class Application {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Application().run();
|
new Application().run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run() {
|
private void run() {
|
||||||
System.out.println("欢迎使用图书管理系统 ");
|
System.out.println("欢迎使用图书管理系统 ");
|
||||||
System.out.println("\t v1.0.0");
|
System.out.println("\t v1.0.0");
|
||||||
@ -27,19 +31,18 @@ public class Application {
|
|||||||
if (input == 1) {
|
if (input == 1) {
|
||||||
// 全部
|
// 全部
|
||||||
showAllBook();
|
showAllBook();
|
||||||
}
|
} else if (input == 2) {
|
||||||
// else if (input == 2) {
|
// // 根据编号查询
|
||||||
// // 根据编号查询
|
int id = Integer.parseInt(system.input("请输入要查询的图书编号:")); // 获取输入
|
||||||
// queryBook();
|
queryBook(id);
|
||||||
// } else if (input == 3) {
|
} else if (input == 3) {
|
||||||
// // 添加
|
// // 添加
|
||||||
// addBook();
|
addBook();
|
||||||
// } else if (input == 4) {
|
} else if (input == 4) {
|
||||||
// modifyBook();// 修改
|
modifyBook();// 修改
|
||||||
// } else if (input == 5) {
|
} else if (input == 5) {
|
||||||
// removeBook(); // 删除
|
removeBook(); // 删除
|
||||||
// }
|
} else if (input == 6) {
|
||||||
else if (input == 6) {
|
|
||||||
System.out.println("已经退出系统了");
|
System.out.println("已经退出系统了");
|
||||||
return; // 推出递归
|
return; // 推出递归
|
||||||
} else {
|
} else {
|
||||||
@ -47,11 +50,83 @@ public class Application {
|
|||||||
}
|
}
|
||||||
run();
|
run();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void removeBook() throws SQLException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new SQLException("暂未实现此方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void modifyBook() throws SQLException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new SQLException("暂未实现此方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
private void queryBook(int bookId) throws SQLException {
|
||||||
|
Book b = db.get(Book.class, bookId);
|
||||||
|
if (null == b) {
|
||||||
|
system.println("查询的图书信息不存在");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
system.println("编号:", b.getId());
|
||||||
|
system.println("书名:", b.getName());
|
||||||
|
system.println("作者:", b.getAuthor().getName());
|
||||||
|
system.println("价格:", b.getId());
|
||||||
|
system.println("出版社:", b.getPublisher().getName());
|
||||||
|
system.println("入库时间:", date.format(b.getCreateTime()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addBook() throws SQLException {
|
||||||
|
// int id = Integer.parseInt(system.input("请输入图书编号:"));
|
||||||
|
String name = system.input("请输入图书名称:");
|
||||||
|
int author = getOrAddAuthor(system.input("请输入作者名称:"));
|
||||||
|
float price = Float.parseFloat(system.input("请输入图书价格:"));
|
||||||
|
int publisher = getOrAddPublisher(system.input("请输入图书出版社:"));
|
||||||
|
|
||||||
|
int bookId = db.insert("insert into author (name,author,publisher,price,create_time,update_time) values (?,?,?,?,now(),now())", name,author,publisher,price);
|
||||||
|
if(bookId > 0) {
|
||||||
|
system.println("新增图书成功,编号为:",bookId);
|
||||||
|
}else{
|
||||||
|
system.error("添加图书失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取作者的编号
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
private int getOrAddAuthor(String name) throws SQLException {
|
||||||
|
Author b = db.find(Author.class, "select * from author where name=?", name);
|
||||||
|
if (b != null) {
|
||||||
|
return b.getId();
|
||||||
|
}
|
||||||
|
return db.insert("insert into author (name,create_time,update_time) values (?,now(),now())", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取出版社的编号
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
private int getOrAddPublisher(String name) throws SQLException {
|
||||||
|
Publisher b = db.find(Publisher.class, "select * from publisher where name=?", name);
|
||||||
|
if (b != null) {
|
||||||
|
return b.getId();
|
||||||
|
}
|
||||||
|
return db.insert("insert into publisher (name,create_time,update_time) values (?,now(),now())", name);
|
||||||
|
}
|
||||||
|
|
||||||
private void showAllBook() throws SQLException {
|
private void showAllBook() throws SQLException {
|
||||||
|
|
||||||
if (db.count("book") == 0) {
|
if (db.count("book") == 0) {
|
||||||
@ -66,5 +141,4 @@ public class Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class DbUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthor() throws Exception {
|
public void testAuthor() throws Exception {
|
||||||
Author t = db.get(Author.class, "select * from author where id = ?",1);
|
Author t = db.get(Author.class,1);
|
||||||
// assertNotNull(t);
|
// assertNotNull(t);
|
||||||
if(t != null) {
|
if(t != null) {
|
||||||
assertEquals(1, t.getId());
|
assertEquals(1, t.getId());
|
||||||
|
@ -25,17 +25,17 @@ public class DbUtil {
|
|||||||
private static Connection conn;
|
private static Connection conn;
|
||||||
private PreparedStatement ps;
|
private PreparedStatement ps;
|
||||||
private ResultSet rs;
|
private ResultSet rs;
|
||||||
// private static DbUtil dbUtil = null;
|
// private static DbUtil dbUtil = null;
|
||||||
|
|
||||||
public static DbUtil createDBUtil() {
|
public static DbUtil createDBUtil() {
|
||||||
return createDBUtil("jdbc:mysql://127.0.0.1:3306/books", "root", "123456");
|
return createDBUtil("jdbc:mysql://127.0.0.1:3306/book", "root", "123456");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DbUtil createDBUtil(String url, String user, String pwd) {
|
public static DbUtil createDBUtil(String url, String user, String pwd) {
|
||||||
// if (dbUtil == null) {
|
// if (dbUtil == null) {
|
||||||
// dbUtil =;
|
// dbUtil =;
|
||||||
// }
|
// }
|
||||||
return new DbUtil(url, user, pwd);
|
return new DbUtil(url, user, pwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DbUtil(String url, String user, String pwd) {
|
private DbUtil(String url, String user, String pwd) {
|
||||||
@ -105,6 +105,28 @@ public class DbUtil {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入数据并获取新增数据的自增ID
|
||||||
|
*
|
||||||
|
* @param sql
|
||||||
|
* @param params
|
||||||
|
* @return
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
public <T> int insert(String sql, Object... params) throws SQLException {
|
||||||
|
prepareStatement(sql, params);
|
||||||
|
int count = ps.executeUpdate();
|
||||||
|
if (count > 0) {
|
||||||
|
// 获取自增id
|
||||||
|
rs = ps.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.dispose();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
private void prepareStatement(String sql, Object... params) throws SQLException {
|
private void prepareStatement(String sql, Object... params) throws SQLException {
|
||||||
ps = conn.prepareStatement(sql);
|
ps = conn.prepareStatement(sql);
|
||||||
if (params.length > 0) {
|
if (params.length > 0) {
|
||||||
@ -120,7 +142,7 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
public <T> T get(Class cls, String sql, Object... params) throws SQLException {
|
public <T> T find(Class cls, String sql, Object... params) throws SQLException {
|
||||||
executeQuery(sql, params);
|
executeQuery(sql, params);
|
||||||
T t = null;
|
T t = null;
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@ -130,6 +152,26 @@ public class DbUtil {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T get(Class<?> cls, Object id) throws SQLException {
|
||||||
|
|
||||||
|
Table table = cls.getAnnotation(Table.class);
|
||||||
|
if (table == null)
|
||||||
|
return null;
|
||||||
|
String tableName = table.name();
|
||||||
|
if (tableName == null || "".equals(tableName)) {
|
||||||
|
tableName = cls.getSimpleName();
|
||||||
|
}
|
||||||
|
String sql = "select * from " + tableName + " where " + table.pk() + "=?";
|
||||||
|
executeQuery(sql, id);
|
||||||
|
T t = null;
|
||||||
|
if (rs.next()) {
|
||||||
|
t = (T) convertToBean(cls);
|
||||||
|
}
|
||||||
|
this.dispose();
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
public <T> List<T> queryList(Class cls, String sql, Object... params) throws SQLException {
|
public <T> List<T> queryList(Class cls, String sql, Object... params) throws SQLException {
|
||||||
executeQuery(sql, params);
|
executeQuery(sql, params);
|
||||||
@ -149,33 +191,35 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd
|
||||||
|
// HH:mm:ss");
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private boolean isDBNull(ResultSet rs,String key) {
|
private boolean isDBNull(ResultSet rs, String key) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> Object getValueFromResult(BeanProperty p, Field f, T t) throws SQLException, ClassNotFoundException {
|
private <T> Object getValueFromResult(BeanProperty p, Field f, T t) throws SQLException, ClassNotFoundException {
|
||||||
// String[] basicType = { "int", "float", "short", "long", "java.lang.String" };
|
// String[] basicType = { "int", "float", "short", "long", "java.lang.String" };
|
||||||
String type = p.type.toLowerCase();
|
String type = p.type.toLowerCase();
|
||||||
if (type.equals("int")) { // 基本类型
|
if (type.equals("int")) { // 基本类型
|
||||||
return rs.getInt(p.field);
|
return rs.getInt(p.field);
|
||||||
}else if (type.equals("float")) { // 基本类型
|
} else if (type.equals("float")) { // 基本类型
|
||||||
return rs.getFloat(p.field);
|
return rs.getFloat(p.field);
|
||||||
}else if (type.equals("double")) { // 基本类型
|
} else if (type.equals("double")) { // 基本类型
|
||||||
return rs.getDouble(p.field);
|
return rs.getDouble(p.field);
|
||||||
}else if (type.equals("short")) { // 基本类型
|
} else if (type.equals("short")) { // 基本类型
|
||||||
return rs.getShort(p.field);
|
return rs.getShort(p.field);
|
||||||
}else if (type.equals("long")) { // 基本类型
|
} else if (type.equals("long")) { // 基本类型
|
||||||
return rs.getLong(p.field);
|
return rs.getLong(p.field);
|
||||||
}else if (type.equals("java.lang.string")) { // 基本类型
|
} else if (type.equals("java.lang.string")) { // 基本类型
|
||||||
return rs.getString(p.field);
|
return rs.getString(p.field);
|
||||||
} else if (type.equals("java.util.date")) { // 日期类型
|
} else if (type.equals("java.util.date")) { // 日期类型
|
||||||
Object v = rs.getObject(p.field);
|
Object v = rs.getObject(p.field);
|
||||||
if(null == v) return null;
|
if (null == v)
|
||||||
|
return null;
|
||||||
return new Date(rs.getDate(p.field).getTime());
|
return new Date(rs.getDate(p.field).getTime());
|
||||||
} else {
|
} else {
|
||||||
String pk = p.pk;
|
String pk = p.pk;
|
||||||
@ -184,12 +228,14 @@ public class DbUtil {
|
|||||||
}
|
}
|
||||||
Class<?> cls = Class.forName(p.type);
|
Class<?> cls = Class.forName(p.type);
|
||||||
Table table = cls.getAnnotation(Table.class);
|
Table table = cls.getAnnotation(Table.class);
|
||||||
if(table == null) return null;
|
if (table == null)
|
||||||
|
return null;
|
||||||
String tableName = table.name();
|
String tableName = table.name();
|
||||||
if (tableName == null || "".equals(tableName)) {
|
if (tableName == null || "".equals(tableName)) {
|
||||||
tableName = cls.getSimpleName();
|
tableName = cls.getSimpleName();
|
||||||
}
|
}
|
||||||
return createDBUtil().get(cls, "select * from " + tableName + " where " + pk + "=?", rs.getObject(p.field));
|
return createDBUtil().find(cls, "select * from " + tableName + " where " + pk + "=?",
|
||||||
|
rs.getObject(p.field));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,13 +267,18 @@ public class DbUtil {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getField(String sql, Object... params) throws SQLException {
|
||||||
|
executeQuery(sql, params);
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getObject(1);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public int count(String tableName) {
|
public int count(String tableName) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
try {
|
try {
|
||||||
executeQuery("select count(*) as total_count from " + tableName);
|
count = (int) getField("select count(*) as total_count from " + tableName);
|
||||||
if (rs.next()) {
|
|
||||||
count = rs.getInt(1);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -8,5 +8,6 @@ import java.lang.annotation.Target;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
public @interface Table {
|
public @interface Table {
|
||||||
public String name();
|
public String name() default "";
|
||||||
|
public String pk() default "id";
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ public class system {
|
|||||||
return input(null);
|
return input(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
public static String input(String msg) {
|
public static String input(String msg) {
|
||||||
if (null != msg) {
|
if (null != msg) {
|
||||||
System.out.print(msg);
|
System.out.print(msg);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user