private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL =
"jdbc:mysql://ip:portNumber/jsp";
private static final String DB_ID = "id";
private static final String DB_PW = "password";
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private static ADao instance;
public static MemberDao getInstance() {
if(instance == null) {
instance = new ADao();
}
return instance;
}
private ADao() {
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("ADao Create Error");
e.printStackTrace();
}
}
private void createConnection() {
try {
connection = DriverManager.getConnection
(DB_URL, DB_ID, DB_PW);
} catch (SQLException e) {
System.out.println("Connection Error);
e.printStackTrace();
}
}
private void closeConnection() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void closePreparedStatement() {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void closeResultSet() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
createConnection();
String sql = "INSERT INTO MEMBER(ID,PW,PHONE,NAME)"
+ "VALUES(?,?,?,?)";
int result = 0;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, member.getId());
preparedStatement.setString(2, member.getPw());
preparedStatement.setString(3, member.getPhone());
preparedStatement.setString(4, member.getName());
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("ADao insert error");
e.printStackTrace();
} finally {
closePreparedStatement();
closeConnection();
}
return result;
}
}
댓글 없음:
댓글 쓰기