?? db2studentdao.java
字號:
package com.ibm.ta.dao.db2;
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.ibm.ta.dao.StudentDAO;
import com.ibm.ta.dao.DAOException;
import com.ibm.ta.dao.DAOParameterException;
import com.ibm.ta.dao.DAOUtil;
import com.ibm.ta.webservice.Student;
public class DB2StudentDAO implements StudentDAO {
private DB2DAOFactory factory;
public DB2StudentDAO(DB2DAOFactory factory) {
this.factory = factory;
}
public Student[] selectStudents() throws DAOException {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List results = new ArrayList();
try {
con = factory.createConnection();
ps = con
.prepareStatement("SELECT student_id, student_name from ta.STUDENT");
rs = ps.executeQuery();
while (rs.next()) {
Student student = new Student(rs.getLong(1), rs.getString(2));
results.add(student);
}
} catch (SQLException sqle) {
throw new DAOException(sqle.getMessage(), sqle);
} finally {
DAOUtil.closeResources(rs, ps, null, con);
}
return (Student[]) results.toArray(new Student[results.size()]);
}
public void insertStudent(String name) throws DAOException,
DAOParameterException {
Connection con = null;
PreparedStatement ps = null;
// Check the parameter
if (name == null || name.trim().length() == 0) {
throw new DAOParameterException(
"Parameter student name is invalid or null");
}
try {
con = factory.createConnection();
ps = con
.prepareStatement("INSERT INTO student (student_name) VALUES (?)");
ps.setString(1, name);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new DAOException(sqle.getMessage(), sqle);
} finally {
DAOUtil.closeResources(null, ps, null, con);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -