package entity;
public class Teacher {
private int id;
private String name;
private String sex;
private String address;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getAddress() {
return address;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
package Dao;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DButil {
static Connection conn = null;
static Statement stat = null;
static PreparedStatement pstm = null;
static ResultSet rs = null;
public static String FileName = "wu1/jdbc";
public static String url;
public static String drive;
public static String name;
public static String password;
public static int executeUpdate(String sql, Object... params) {
int rows = 0;
try {
conn = getConnection();
pstm = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstm.setObject(i + 1, params[i]);
}
rows = pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeALL(rs, stat, conn);
}
return rows;
}
public static void loadFild() {
Properties prop = new Properties();
try {
prop.load(new FileReader(FileName));
drive = prop.getProperty("drive");
url = prop.getProperty("url");
name = prop.getProperty("name");
password = prop.getProperty("password");
System.out.println(drive); System.out.println(url);
System.out.println(name); System.out.println(password);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
try {
loadFild();
Class.forName(drive);
conn = DriverManager.getConnection(url, name, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void closeALL(ResultSet rs, Statement stat, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
url = jdbc:oracle:thin:@127.0.0.1:1521:orcl
drive = oracle.jdbc.driver.OracleDriver
name =scott
password = tiger
package impl;
import entity.Teacher;
import java.sql.SQLException;
import java.util.List;
public interface TeachDao {
int add(Teacher teacher) throws SQLException;
int delete(int id);
int update(Teacher teacher);
List<Teacher> findAll();
}
package impl;
import Dao.DButil;
import entity.Teacher;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TeacherDaoimpl implements TeachDao {
Connection conn = null;
PreparedStatement stat =null;
ResultSet rs = null;
@Override
public int add(Teacher teacher) {
int i = 0;
try {
conn = DButil.getConnection();
String sql="INSERT INTO teacher(id,name,sex,address)VALUES(?,?,?,?)";
stat = conn.prepareStatement(sql);
stat.setInt(1, teacher.getId());
stat.setString(2,teacher.getName());
stat.setString(3,teacher.getSex());
stat.setString(4,teacher.getAddress());
i = stat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DButil. closeALL(rs, stat, conn);
}
return i;
}
@Override
public int delete(int id) {
int i = 0;
try {
conn = DButil.getConnection();
String sql="DELETE FROM teacher WHERE id = ?";
stat = conn.prepareStatement(sql);
stat.setInt(1,id);
i = stat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public int update(Teacher teacher) {
int i = 0;
try {
conn = DButil.getConnection();
String sql="update teacher set name=? ,sex=?,address=? WHERE id = ? ";
stat = conn.prepareStatement(sql);
stat.setString(1,teacher.getName());
stat.setString(2,teacher.getSex());
stat.setString(3,teacher.getAddress());
stat.setInt(4,teacher.getId());
i = stat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public List<Teacher> findAll() {
List<Teacher> list = new ArrayList<>();
try {
conn = DButil.getConnection();
String sql="select * FROM teacher ";
stat = conn.prepareStatement(sql);
rs = stat.executeQuery();
while (rs.next()){
Teacher teacher = new Teacher();
teacher.setId(rs.getInt("id"));
teacher.setName(rs.getString("name"));
teacher.setSex(rs.getString("sex"));
teacher.setAddress(rs.getString("address"));
list.add(teacher);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list.size() >0 ? list :null;
}
}
package Test;
import entity.Teacher;
import impl.TeachDao;
import impl.TeacherDaoimpl;
import java.sql.SQLException;
import java.util.List;
public class Test {
public static void main(String[] args) throws SQLException {
Teacher teacher = new Teacher();
teacher.setId(15);
teacher.setName("we");
teacher.setSex("男");
teacher.setAddress("we");
TeachDao teachDao = new TeacherDaoimpl();
int add = teachDao.add(teacher);
if(add>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
TeachDao teachDao1 = new TeacherDaoimpl();
int a = teachDao1.delete(15);
if(a>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
TeachDao teachDao2 = new TeacherDaoimpl();
List<Teacher> all = teachDao2.findAll();
for (Teacher s:all
) {
System.out.println(s);
}
TeachDao teachDao3 = new TeacherDaoimpl();
Teacher teacher1 = new Teacher();
teacher1.setId(13);
teacher1.setName("克林");
teacher1.setSex("男");
teacher1.setAddress("龟仙屋");
int a1 = teachDao3.update(teacher1);
if(a1>0){
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
}
}