日期:2014-05-20 浏览次数:20742 次
package da;
import domain.MemberData;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Date;
public class MemberDataDA {
private String host = "jdbc:derby://localhost:1527/memberdb";
private String user = "nbuser";
private String password = "nbuser";
private String tableName = "MEMBERDATA";
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
public MemberDataDA() {
createConnection();
}
public void addMember(MemberData Data) {
//convert char to string
try {
String password = String.valueOf(Data.getPassword());
String insertStr = "INSERT INTO " + tableName + " Values(?,?,?,?,?,?,?,?,?,?) ";
stmt = conn.prepareStatement(insertStr);
stmt.setString(1, Data.getName());
stmt.setString(2, Data.getStudentID());
stmt.setInt(3, Data.getMemberID());
stmt.setString(4, Data.getGender());
stmt.setString(5, Data.getSchool());
stmt.setString(6, Data.getProgramme());
stmt.setString(7, Data.getContact_Num());
stmt.setString(8, Data.getJoin_date());
stmt.setString(9, Data.getLeave_date());
stmt.setString(10, password);
stmt.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void UpdateMember(MemberData data) {
try {
String password = String.valueOf(data.getPassword());
stmt = conn.prepareStatement("UPDATE " + tableName + " SET NAME = ?, STUDENTID = ?, GENDER=?, SCHOOL=?, PROGRAMME=?, CONTACT_NUMBER=?, PASSOWORD=? WHERE MEMBERID = ? ");
stmt.setString(1, data.getName());
stmt.setString(2, data.getStudentID());
stmt.setString(3, data.getGender());
stmt.setString(4, data.getSchool());
stmt.setString(5, data.getProgramme());
stmt.setString(6, data.getContact_Num());
stmt.setString(7, password);
stmt.setInt(8, data.getMemberID());
stmt.executeUpdate();
} catch (SQLException ex) {
}
}
private void createConnection() {
try {
conn = DriverManager.getConnection(host, user, password);
System.out.println("***TRACE: Connection established.");
} catch (SQLException ex) {