实现的功能为8个模块:1.增加 2.查询所有信息 3.通过某些字段查询 4.删除 5.修改 6.查询数据库信息总条数
7.分页 8.like查询
实现方式为 :通过接口定义方法,创建学生类,再创建一个类去implements,最后通过测试类去实现。(选中方法右键运行JUnit)
出错的原因无非这几种:1.数据库字段与方法字段顺序有误 2.Sql语句有问题 3.导包出错。(记得导入mysql-connector-java-5.1.40.jar和junit.jar)
这是数据库信息:
学生类:StudentX
public class StudentX {
private int Xid;
private String name;
private String pwd;
private String school;
private int age;
private String phone;
public int getXid() {
return Xid;
}
public void setXid(int xid) {
Xid = xid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "StudentX [Xid=" + Xid + ", name=" + name + ", pwd=" + pwd + ", school=" + school + ", age=" + age
+ ", phone=" + phone + "]";
}
}
JDBC连接:DBUtil
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
static private Connection conn;
static private String driver="com.mysql.jdbc.Driver";
static private