第一步:导入需要的jar编写db文件
image.png第二步:根据数据库文件创建实体类
学生信息实体类,不同表可在此包下创建新的实体类package Student.Entity;
public class Student {
private int stuid;
private String stunameString;
@Override
public String toString() {
return "Student [stuid=" + stuid + ", stunameString=" + stunameString + "]";
}
public Student() {}
public Student(int stuid, String stunameString) {
super();
this.stuid = stuid;
this.stunameString = stunameString;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStunameString() {
return stunameString;
}
public void setStunameString(String stunameString) {
this.stunameString = stunameString;
}
}
第三步:创建Jdbc工具类JdbcUtil
JdbcUtil工具类,用于与数据库连接package Student.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JdbcUtil {
private static String url="";
private static String user="";
private static String psw="";
static {
ResourceBundle db = ResourceBundle.getBundle("db");
String Driver = db.getString("Driver");
url = db.getString("url");
user = db.getString("user");
psw = db.getString("psw");
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//静态方法获得con
public static Connection getConnection() {
Connection con=null;
//创建连接
try {
con = DriverManager.getConnection(url,user,psw);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//释放资源
public static void relese(ResultSet rs,Statement st,Connection con) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
第四步:创建接口类Student.Dao
package Student.Dao;
import java.util.List;
import Student.Entity.Student;
public interface StudentDao {
public int insert(Student s);//插入接口方法,参数为一个学生 返回int作为判断
public int deldete(int id);//删除接口方法,以一个学生的stuid作为参数 返回int作为判断
public int updata(Student t);//更新接口方法
public List<Student> findAll();//获得所有学生
public Student findStudentByid(String stuid);//根据学号获得学生
}
第五步:创建接口实现类
新建StudentDaoImpl类,接口选择StudentDaoimage.png
StudentDaoImpl代码如下
package Student.Dao.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import Student.Dao.StudentDao;
import Student.Entity.Student;
import Student.Util.JdbcUtil;
/*
* 以下方法作为接口类的具体实现
* 需要直接调用
* 方便用户开发
*/
public class StudentDaoImpl implements StudentDao {
@Override
public int insert(Student s) {
int i=0;
// TODO Auto-generated method stub
Connection connection=JdbcUtil.getConnection();
String sql="insert into student (stuid,stuname) values (?,?)";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(0, s.getStuid());
ps.setString(1,s.getStuname());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public int deldete(String id) {
int i=0;
Connection connection=JdbcUtil.getConnection();
String sql="delete form student where id="+id;
try {
Statement st = connection.createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
//重写方法,更新学生信息
@Override
public int updata(Student t,String stuid) {
int i=0;
Connection con=JdbcUtil.getConnection();
String sql="update student set stuid='"+t.getStuid()+"',stuname='"+t.getStuname()+"' where stuid='"+stuid+"'";
try {
Statement st = con.createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return i;
}
//重写接口方法,获得所有学生
@Override
public List<Student> findAll() {
List<Student> list=new ArrayList<Student>();
Connection connection=JdbcUtil.getConnection();
String sql="select * form student";
try {
Statement st = connection.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {
Student student=new Student(rs.getString(0), rs.getString(1));
list.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//重写接口方法,根据id获得单个学生
@SuppressWarnings("null")
@Override
public Student findStudentByid(String stuid) {
Student student = null;
Connection connection=JdbcUtil.getConnection();
String sql= "select form student where stuid="+stuid;
try {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(sql);
if(rs!=null) {
student.setStuid(rs.getString(0));
student.setStuname(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
}
新建Student.Service接口类和实现类如下
StudentService代码如下
package Student.Service;
import java.util.List;
import Student.Entity.Student;
public interface StudentService {
public int insert(Student s);
public int delete(String stuid);
public int update(Student s,String stuid);
public List<Student> findAll();//获得所有学生
public Student findStudentByid(String stuid);//根据学号获得学生
}
StudentServiceImpl代码如下
package Student.Service.Impl;
import java.util.List;
import Student.Dao.StudentDao;
import Student.Dao.Impl.StudentDaoImpl;
import Student.Entity.Student;
import Student.Service.StudentService;
public class StudentServiceImpl implements StudentService {
private StudentDao studentdao=new StudentDaoImpl();
public int insert(Student s) {
int i=studentdao.insert(s);
return i;
}
@Override
public int delete(String stuid) {
int i=studentdao.deldete(stuid);
return i;
}
@Override
public int update(Student s, String stuid) {
int i=studentdao.updata(s, stuid);
return i;
}
@Override
public List<Student> findAll() {
List<Student> list=studentdao.findAll();
return list;
}
@Override
public Student findStudentByid(String stuid) {
Student student=studentdao.findStudentByid(stuid);
return student;
}
}
网友评论