这一长比较重要设计的到数据库,我们上三的时候说到界面添加按钮之后进入界面添加的操作,我这里对JDBC进行了封装,当然连接JDBC有很多的技术,我们这里采用的properties方式,这种方式方便开发者管理,并且对初学者来说容易操作.
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/soft1701
user=root
pwd=root
封装的BaseDao代码如下,里面封装了打开数据,以及增删改查的方法,方便学生操作
public class BaseDao {
private Connection con;
private PreparedStatement pstm;
private ResultSet rst;
private static String driver;
private static String url;
private static String user;
private static String pwd;
static{
Properties prop = new Properties();
try {
// 加载属性文件
prop.load(BaseDao.class.getClassLoader().getResourceAsStream("dao/dbinfo.properties"));
// 读取属性文件中的属性
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
pwd = prop.getProperty("pwd");
// 加载驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取连接
public Connection getConnection(){
try {
con = DriverManager.getConnection(url, user, pwd);
return con;
}catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<Student> QueryAllStudents() {
Statement stt = null;
ResultSet rs = null;
List<Student> students = new ArrayList<Student>();
try {
stt = (Statement) con.createStatement();
rs=stt.executeQuery("select * from students");
while(rs.next()){
int id = rs.getInt("_id");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String phone = rs.getString("phone");
String address = rs.getString("address");
Student student = new Student(name,sex,age,phone,address);
students.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("查询失败");
}finally{
try {
stt.close();
rs.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return students;
}
/**
* 添加数据
*
* */
public boolean AddStudentInfor( Student student) {
boolean result;
String mysql = "insert into students(name,sex,age,phone,address) values(?,?,?,?,?)";
PreparedStatement ptt = null;
try {
ptt = (PreparedStatement) con.prepareStatement(mysql);
ptt.setString(1, student.getName());
ptt.setString(2, student.getSex());
ptt.setInt(3, student.getAge());
ptt.setString(4, student.getPhone());
ptt.setString(5, student.getAddress());
ptt.execute();
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = false;
}finally {
try {
ptt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public boolean DeleteStudentInfor( int _id) {
boolean result;
String sql="delete from students where _id=?";
PreparedStatement pst = null;
try {
pst = (PreparedStatement) con.prepareStatement(sql);
pst.setInt(1, _id);
pst.execute();
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = false;
} finally {
try {
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public static boolean UpdateStudentInfor(Connection connection, Student student) {
boolean result;
String sql="update students set name=?,sex=?,age=?,phone=?,address=? where _id = ?";
PreparedStatement pst = null;
try {
pst=(PreparedStatement) connection.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getSex());
pst.setInt(3, student.getAge());
pst.setString(4, student.getPhone());
pst.setString(5, student.getAddress());
pst.setInt(6, student.getId());
pst.execute();
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = false;
} finally {
try {
pst.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public void closeAll(){
if(rst!=null){
try {
rst.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();
}
}
}
}