发表于: 2020-05-30 23:29:52
1 1612
今日作为:写任务1-17,写到下午写完了第一遍,然后发现思路不对,又重新规划了,今天还没有写完,明天继续
表结构:
实体类:
package com.jnshu.demo.vo;
public class Myemp
{
private String name;
private String school;
private String online_un;
private int number;
private String city;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSchool(){
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getOnline_un(){
return online_un;
}
public void setOnline_un(String online_un) {
this.online_un = online_un;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
实现类:还没写完
package com.jnshu.demo.impl;
import com.jnshu.demo.dao.IMYempDAO;
import com.jnshu.demo.vo.Myemp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class MyempDAOImpl implements IMYempDAO
{
private Connection conn = null;
private PreparedStatement pstmt = null;
public MyempDAOImpl(Connection conn){
this.conn = conn;
}
public List<Myemp> finALL(String keyWord) throws Exception{
List<Myemp> all = new ArrayList<Myemp>();
String sql = "select name,school,online_un,number,city from students";
this.pstmt = this.conn.prepareStatement(sql);
ResultSet rs = this.pstmt.executeQuery();
Myemp emp = null;
while (rs.next()){
emp = new Myemp();
emp.getName(rs.getString(1));
emp.getSchool(rs.getString(2));
emp.getOnline_un(rs.getString(3));
emp.setNumber(rs.getInt(4));
emp.setCity(rs.getString(5));
all.add(emp);
this.pstmt.close();
return all;
}
}
}
接口:
package com.jnshu.demo.dao;
import com.jnshu.demo.vo.Myemp;
import java.util.List;
public interface IMYempDAO
{
public List<Myemp> findALL(String keyWord) throws Exception;
}
连接关闭数据库操作:
package com.jnshu.demo.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection
{
private static final String driver = "jdbc.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/it-xzy";
private static final String user = "root";
private static final String password = "54229103w";
private Connection conn = null;
public DatabaseConnection() throws Exception{
Class.forName(driver);
this.conn = DriverManager.getConnection(url,user,password);
}
public Connection getConnection(){
return this.conn;
}
public void close() throws Exception{
if(this.conn != null){
try {
this.conn.close();
}catch (Exception e){
throw e;
}
}
}
}
明天计划:接着继续搞完
今日疑惑:还是没怎么弄明白DAO
评论