暑假第一周总结
数据库小学期实验:
(部分)
二、数据库设计
1.admin(登录表)
属性名 |
属性含义 |
数值类型 |
是否可为空 |
是否为主码 |
是否引用外码 |
user_name |
用户名 |
varchar(255) |
Yes |
No |
No |
pwd |
密码 |
varchar(255) |
Yes |
No |
No |
id |
序号 |
Int |
Yes |
No |
No |
type |
类别 |
varchar(255) |
Yes |
No |
No |
2.courier(员工信息表)
属性名 |
属性含义 |
数值类型 |
是否可为空 |
是否为主码 |
是否引用外码 |
name |
姓名 |
varchar(255) |
No |
Yes |
No |
cid |
员工编号 |
varchar(255) |
Yes |
No |
No |
phone |
电话 |
varchar(255) |
Yes |
No |
No |
id |
序号 |
int |
Yes |
No |
No |
3.student(商品订货出货表)
属性名 |
属性含义 |
数值类型 |
是否为空 |
是否为主码 |
是否引用外码 |
name |
姓名 |
Varchar(255) |
Not null |
Yes |
No |
Student_number |
学号 |
Varchar(255) |
Null |
No |
No |
Receiving_address |
地址 |
Varchar(255) |
Null |
No |
No |
phone |
电话 |
Varchar(255) |
Null |
No |
No |
id |
序号 |
Int |
Null |
No |
No |
4.rxpress(快递基本信息表)
属性名 |
属性含义 |
数值类型 |
是否为空 |
是否为主码 |
是否引用外码 |
name |
名称 |
Varchar(255) |
Not null |
Yes |
No |
Sid |
学生编号 |
Varchar(255) |
Not null |
Yes |
Yes |
Cid |
员工编号 |
Varchar(255) |
Not Null |
Yes |
Yes |
id |
编号 |
Int |
Null |
No |
No |
链接数据库:
package until; import java.sql.*; public class Dbutil { /** * 完成与数据库的连接和数据的访问 * @author Leiyu * @version 1.0 * */ private String driverStr = "com.mysql.jdbc.Driver"; private String connStr = "jdbc:mysql://localhost:3306/sqltext?uerUnicode=true&characterEncoding=UTF-8"; private String dbusername = "root"; private String dbpassword = "123456"; private Connection conn = null; private Statement stmt = null; { try { Class.forName(driverStr); conn = DriverManager.getConnection(connStr, dbusername, dbpassword); stmt = conn.createStatement(); } catch (Exception ex) { System.out.println("数据库连接失败!"); } } /** * 执行更新操作 * @param s * SQL语句 * @return * 更新操作的结果 */ public int executeUpdate(String s) { int result = 0; try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.out.println("更新出现异常!"); } return result; } /** * 执行查询操作 * @param s * SQL语句 * @return * 查询结果 */ public ResultSet executeQuery(String s) { ResultSet rs = null; try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.out.println("查询出现异常!"); } return rs; } /** * 关闭数据库 */ public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
adminBean:
package Bean; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import until.Dbutil; import Bean.adminBean; public class adminBean { private String user_name; private String pwd; private String type; private int id; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public int getId() { return id; } public void setId(int id) { this.id = id; } /** * 从BookInfo表中获取所有的图书信息 * * @return BookInfo的数组 */ public static ArrayList<adminBean> getAdminList() { ArrayList<adminBean> list = new ArrayList<adminBean>(); String sql = "select * from admin"; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { adminBean bi = new adminBean(); bi.setUser_name(rs.getString("username")); bi.setPwd(rs.getString("pwd")); bi.setType(rs.getString("type")); bi.setId(rs.getInt("id")); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的图书信息 * * @param id 图书id * @return 一个BookInfo对象 */ public static adminBean getAdminById(String username) { String sql = "select * from admin where user_name=" + username; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); adminBean bi = new adminBean(); try { if (rs.next()) { bi.setUser_name(rs.getString("user_name")); bi.setType(rs.getString("type")); bi.setPwd(rs.getString("pwd")); bi.setId(rs.getInt("id")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return bi; } /** * 更新指定id的图书信息 * * @param bi 要更新的图书的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateAdmin(adminBean bi) { int result = 0; String sql = "update admin set user_name='" + bi.getUser_name() + "',pwd='" + bi.getPwd() + "',birthday='" + bi.getType() + "' where id='" + bi.getId()+"'"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 删除指定id的图书 * * @param id 图书id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteAdmin(String id) { int result = 0; String sql = "delete from admin where id=" + id; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 增加一本图书 * * @param bi 图书对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addAdmin(adminBean bi) { int result = 0; String sql = "insert into admin(id,user_name,pwd,type) values('" + bi.getId() + "','"+ bi.getUser_name() + "','" + bi.getPwd() + "','" + bi.getType() + "')"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } }
courierBean:
package Bean; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import until.Dbutil; import Bean.courierBean; public class courierBean { private String name; private String cid; private String phone; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public int getId() { return id; } public void setId(int id) { this.id = id; } /** * 从BookInfo表中获取所有的图书信息 * * @return BookInfo的数组 */ public static ArrayList<courierBean> getCourierList() { ArrayList<courierBean> list = new ArrayList<courierBean>(); String sql = "select * from courier"; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { courierBean bi = new courierBean(); bi.setName(rs.getString("name")); bi.setCid(rs.getString("cid")); bi.setPhone(rs.getString("phone")); bi.setId(rs.getInt("id")); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的图书信息 * * @param id 图书id * @return 一个BookInfo对象 */ public static courierBean getCourierById(int id) { String sql = "select * from courier where id=" + id; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); courierBean bi = new courierBean(); try { if (rs.next()) { bi.setName(rs.getString("name")); bi.setCid(rs.getString("cid")); bi.setPhone(rs.getString("phone")); bi.setId(rs.getInt("id")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return bi; } /** * 更新指定id的图书信息 * * @param bi 要更新的图书的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateAdmin(courierBean bi) { int result = 0; String sql = "update admin set name='" + bi.getName() + "',cid='" + bi.getCid() + "',phone='" + bi.getPhone() + "' where id='" + bi.getId()+"'"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 删除指定id的图书 * * @param id 图书id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteCourier(int id) { int result = 0; String sql = "delete from courier where id=" + id; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 增加一本图书 * * @param bi 图书对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addCourier(courierBean bi) { int result = 0; String sql = "insert into courier(name,cid,phone,id) values('" + bi.getName() + "','"+ bi.getCid() + "','" + bi.getPhone() + "','" + bi.getId() + "')"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } }