日期:2014-05-17 浏览次数:20738 次
// user 添加修改 public static boolean updateUser(int userID, String username, String password, String sex, String email, String qq, String phone) { /* * update user information */ Connection conn = null; boolean flag = false; try { conn = DB.getConnection(); PreparedStatement ptmt = conn .prepareStatement("update user " + "set username=?, password=?, sex=?, email=?, qq=?, phone=?" + " where userID=?"); ptmt.setString(1, username); ptmt.setString(2, password); ptmt.setString(3, sex); ptmt.setString(4, email); ptmt.setString(5, qq); ptmt.setString(6, phone); ptmt.setInt(7, userID); ptmt.executeUpdate(); flag = true; } catch (Exception e) { flag = false; e.printStackTrace(); } finally { DB.closeConnection(conn); } return flag; } //user删除 public static boolean deleteUser(int userID) { boolean flag = false; Connection conn = null; try { conn = DB.getConnection(); PreparedStatement ptmt = conn .prepareStatement("delete from user where userID = ?"); ptmt.setInt(1, userID); ptmt.executeUpdate(); ptmt.close(); ptmt = conn.prepareStatement("delete from reply where userID = ?"); ptmt.setInt(1, userID); ptmt.executeUpdate(); ptmt.close(); ptmt = conn.prepareStatement("delete from user where userID = ?"); ptmt.setInt(1, userID); ptmt.executeUpdate(); flag = true; } catch (Exception e) { flag = false; e.printStackTrace(); } finally { DB.closeConnection(conn); } return flag; }
------解决方案--------------------
//DB 类 package com.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DB { public static Connection getConnection() { Connection conn = null; try { String driver = "com.mysql.jdbc.Driver"; String dbURL = "jdbc:mysql://localhost:3306/dbName?useUnicode=true&characterEncoding=UTF-8"; String username = "root"; String password = ""; Class.forName(driver).newInstance(); conn = DriverManager.getConnection(dbURL, username, password); } catch (SQLException e) { // TODO: handle exception e.printStackTrace();