发表于: 2017-04-13 21:52:34

4 1327


今天完成的事情:完成Dao的编写

Student.java

package com.xiuzhen.model;

import java.io.Serializable;
/**
* Created by LALH on 2017/4/12.
*/
public class Student implements Serializable {
private Long id;
private String name;
private String avatar;
private String type;
private String introduction;

public Student() {};
public Student(long id, String name, String avatar, String type, String introduction) {

this.id = id;
this.name = name;
this.avatar = avatar;
this.type = type;
this.introduction = introduction;
}

public long getId(){
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAvatar() {
return avatar;
}

public void setAvatar(String avatar) {
this.avatar = avatar;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getIntroduction() {
return introduction;
}

public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}

StudentDao.java

package com.xiuzhen.model;

import com.xiuzhen.model.Student;
import java.sql.SQLException;
import java.util.List;

/**
* Created by LALH on 2017/4/13.
*/
public interface StudentDao {

public void add(Student student) throws SQLException;

public void update(Student student) throws SQLException;

public int delete(int id) throws SQLException;

public List<Student> select() throws SQLException;

public Student findStudentById();

}

StudentDaolmpl.java

package com.xiuzhen.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.xiuzhen.model.Student;
import com.xiuzhen.model.StudentDao;
import com.xiuzhen.model.Util;
/**
* Created by LALH on 2017/4/13.
*/
public class StudentDaolmpl implements StudentDao {

public void add(Student student) throws SQLException{
Connection connection = Util.getConnection();
String sql = "INSERT into student (name,avatar) values(?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1,student.getName());
pst.setString(2,student.getAvatar());
pst.execute();

}
public void update(Student student) throws SQLException{
Connection connection = Util.getConnection();
String sql = "update student set name = ? , avatar = ? where id= ?";
System.out.println(sql);
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1,student.getName());
pst.setString(2,student.getAvatar());
pst.setLong(3,student.getId());
pst.executeUpdate();
}
public int delete(int id) throws SQLException{
Connection connection = Util.getConnection();
String sql = "delete from student where id= ? ";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setInt(1,id);
pst.execute();
return id;
}
public List<Student> select() throws SQLException{
Connection connection = Util.getConnection();
String sql = "select * from student";
PreparedStatement pst = connection.prepareStatement(sql);
Student student = new Student();
ResultSet rs = pst.executeQuery();
List<Student> list = new ArrayList<Student>();

while (rs.next()){
student.setName(rs.getString(1));
student.setId(rs.getLong(2));
student.setAvatar(rs.getString(3));
list.add(student);

}
return list;
}
public Student findStudentById(){
return null;
}
}

Util.java

package com.xiuzhen.model;

/**
* Created by LALH on 2017/4/13.
*/

import java.sql.Connection;
import java.sql.DriverManager;
public class Util {
private static Connection connection = null;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/xiuzhen";
private static final String USERNAME = "liang";
private static final String PASSWORD = "liang";

static {
try {
Class.forName(DRIVER);
connection = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
}

public static Connection getConnection(){
return connection;
}

//关闭数据库
   public void close() throws Exception{
if (this.connection != null){
try {
this.connection.close();
}catch (Exception e){
throw e;
}
}
}
}



明天计划的事情:初步学习JUnit


遇到的问题:数据库连接的设置


收获:学习Dao的编写和数据库的设置




返回列表 返回列表
评论

    分享到