🌈目录
- 🎄建立接口
- 🎨团队开发命名规范解决方案
- 📦封装属性
- 💰完成 insert 功能
- 🎰插入测试
- 🧿完善 return 0
- 🔮修改 select 返回值类型
- 🎲完成 select 功能
- 🎯查询测试
🎄建立接口
interfacer.java
package cc.cnix.ShoppingSystem.interfacer;
import cc.cnix.ShoppingSystem.manager.user_manager;
public interface interfacer {
/**
* 插入学生信息
* @param um 学生信息对象
* @return -2 数据库异常 -1 值存在 0 插入失败 >0 插入成功
*/
public int insertUser(user_manager um); //import user_manager
public void selectUser(user_manager um);
}
🎨团队开发命名规范解决方案
contorl_user.java
package cc.cnix.ShoppingSystem.control;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//团队开发,规范化命名
import cc.cnix.ShoppingSystem.interfacer.interfacer;
import cc.cnix.ShoppingSystem.jdbc.JdbcUnit;
import cc.cnix.ShoppingSystem.manager.user_manager;
public class control_user implements interfacer {
@Override
public void insertUser(user_manager um) {
// TODO Auto-generated method stub
}
@Override
public void selectUser(user_manager um) {
// TODO Auto-generated method stub
}
}
要修改本身的东西需要在接口文件(
interfacer.java
)修改eg : 文档注释
📦封装属性
user_manager.java
Getter and Setters
package cc.cnix.ShoppingSystem.manager;
//对参数进行封装,以后只调用一个就好
public class user_manager {
private String S_id = "";
private String S_Number = "";
private String S_Name = "";
private String S_Sex = "";
public String getS_id() {
return S_id;
}
public void setS_id(String s_id) {
S_id = s_id;
}
public String getS_Number() {
return S_Number;
}
public void setS_Number(String s_Number) {
S_Number = s_Number;
}
public String getS_Name() {
return S_Name;
}
public void setS_Name(String s_Name) {
S_Name = s_Name;
}
public String getS_Sex() {
return S_Sex;
}
public void setS_Sex(String s_Sex) {
S_Sex = s_Sex;
}
}
💰完成 insert 功能
删除 control_user.java 错误地方,重新导入
略
control_user.java
@Override
public int insertUser(user_manager um) {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb(); //打开数据库
ResultSet rs = JdbcUnit.dbSelect(con, "si_user", "s_id = " + um.getS_id());
//利用指针下移判断是否有数据
try {
if (rs.next()) {
return -1; //利用数字定义个人协议
}else {
int w = JdbcUnit.dbInsert(con, "si_user", "" + um.getS_id() + ",'" + um.getS_Number() + "','" + um.getS_Name() + "','" + um.getS_Sex() + "'");
if (w > 0) {
return w;
}else {
return 0;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -2; //定义异常的返回值
}
}
🎰插入测试
UnitTest.java
@Test
public void test() {
control_user cu = new control_user(); //import control_user
user_manager um = new user_manager();
um.setS_id("1");
um.setS_Number("11789");
um.setS_Name("wriprin");
um.setS_Sex("male");
int h = cu.insertUser(um);
if (h == -2) {
System.out.println("服务器异常");
}else if (h == -1) {
System.out.println("所插入数据已存在,请修改后重新插入");
}else if (h == 0) {
System.out.println("程序bug,请联系管理人员修改");
}else {
System.out.println("注册成功");
}
}
🧿完善 return 0
学习使用数字定义个人协议,而非 boolean
(只能表示真假两条件)
control_user.java
try {
if (rs.next()) {
return -1; //利用数字定义个人协议
}else {
int w = JdbcUnit.dbInsert(con, "si_user", "" + um.getS_id() + ",'" + um.getS_Number() + "','" + um.getS_Name() + "','" + um.getS_Sex() + "'");
if (w > 0) {
return w;
}else {
return 0;
}
}
🔮修改 select 返回值类型
interfacer.java
public ArrayList<user_manager> selectUser(user_manager um); //返回值改为 ArrayList
🎲完成 select 功能
control_user.java
@Override
public ArrayList<user_manager> selectUser(user_manager um) {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb(); //打开数据库
ResultSet rs = JdbcUnit.dbSelect(con, "si_user", "s_id = " + um.getS_id());
ArrayList<user_manager> al = new ArrayList<user_manager>();
user_manager dw = new user_manager();
try {
if (rs.next()) {
dw.setS_id(rs.getString(1).toString());
dw.setS_Number(rs.getString(1).toString());
dw.setS_Name(rs.getString(1).toString());
dw.setS_Sex(rs.getString(1).toString());
al.add(dw);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
🥏查询测试
UnitTest.java
@Test
public void testSelect() {
control_user cu = new control_user(); //import control_user
user_manager um = new user_manager();
um.setS_id("1");
ArrayList<user_manager> al = cu.selectUser(um);
if (al.size() > 0) {
System.out.println(al.get(0).getS_id());
System.out.println(al.get(0).getS_Number());
System.out.println(al.get(0).getS_Name());
System.out.println(al.get(0).getS_Sex());
}else {
System.out.println("无内容");
}
}
🧵完成 delete 功能
interfacer.java
public int deleteUser(user_manager um);
public int deleteUserAll();
public int updateUser(user_manager um);
control_user.java
追加新添加的方法
完成删除方法
@Override
public int deleteUser(user_manager um) {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb();
int h = JdbcUnit.dbDelete(con, "si_user", "s_id = " + um.getS_id());
if (h >= 0) {
return 1;
}else {
return 0;
}
}
@Override
public int deleteUserAll() {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb();
int h = JdbcUnit.dbDelete(con, "si_user");
return 0;
}
🎡完成 update 功能
@Override
public int updateUser(user_manager um) {
// TODO Auto-generated method stub
String value = "";
Connection con = JdbcUnit.openDb();
if (um.getS_Name().equals("") == false) {
value = value + "S_Name = '" + um.getS_Name() + "'";
}
if (um.getS_Number().equals("") == false) {
if (value.equals("") == true) {
value = value + "S_Number = '" + um.getS_Number() + "'";
}else {
value = value + ",S_Number = '" + um.getS_Number() + "'";
}
}
if (um.getS_Sex().equals("") == false) {
if (value.equals("") == true) {
value = value + "S_Sex = '" + um.getS_Sex() + "'";
}else {
value = value + ",S_Sex = '" + um.getS_Sex() + "'";
}
}
int h = JdbcUnit.dbUpdate(con, "si_user", value, "S_id = " + um.getS_id());
if (h > 0) {
return 1;
}else {
return 0;
}
}
🎯关闭数据库连接
如果有 try catch 就写在 catch 后面,如果没有 try catch 就写在数据库连接后面。
control_user.java
finally{
JdbcUnit.dbClose(con, null, null);
}
eg1 : (有 try catch)
@Override
public int insertUser(user_manager um) {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb(); //打开数据库
ResultSet rs = JdbcUnit.dbSelect(con, "si_user", "s_id = " + um.getS_id());
//利用指针下移判断是否有数据
try {
if (rs.next()) {
return -1; //利用数字定义个人协议
}else {
int w = JdbcUnit.dbInsert(con, "si_user", "" + um.getS_id() + ",'" + um.getS_Number() + "','" + um.getS_Name() + "','" + um.getS_Sex() + "'");
if (w > 0) {
return w;
}else {
return 0;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -2; //定义异常的返回值
}
finally{
JdbcUnit.dbClose(con, null, null);
}
}
eg 2 : (无 try catch)
@Override
public int deleteUser(user_manager um) {
// TODO Auto-generated method stub
Connection con = JdbcUnit.openDb();
int h = JdbcUnit.dbDelete(con, "si_user", "s_id = " + um.getS_id());
JdbcUnit.dbClose(con, null, null);
if (h >= 0) {
return 1;
}else {
return 0;
}
}