中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>程序设计>Java>列表

简单的数据库操作 增 删 改 查

来源:KingKongII.cublog.cn 作者: 时间:2007-12-08 点击:

  public class TeacherManage

  {

  // 添加语句

  public static final String INSERTTEACHER = "insert into teacher(name,password) values(?,?)";

  

  // 删除语句

  public static final String DELETEATEACHER = "delete from teacher where teaId = ?";

  

  // 修改语句

  public static final String UPDATETEACHER = "update teacher set name = ?,password = ? where teaid = ?";

  

  // 查询语句

  public static final String SELECTTEACHER = "select * from teacher";

  // 添加一条记录

  public static boolean insertTeacher(String name, String password)

  {

  boolean flag = false;

  Connection conn = DBManage.getConnection();

  PreparedStatement pstmt = null;

  int count = 0;

  try

  {

  pstmt = conn.prepareStatement(INSERTTEACHER);

  pstmt.setString(1, name);

  pstmt.setString(2, password);

  count = pstmt.executeUpdate(); // 记录操作执行的记录条数

  if (count != 0)

  {

  flag = true;

  }

  conn.commit();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  finally

  {

  if (null != conn)

  {

  try

  {

  conn.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  if (null != pstmt)

  {

  try

  {

  pstmt.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  }

  return flag;

  }

  

  // 删除一条信息

  public static boolean deleteATEacher(int id)

  {

  boolean flag = false;

  Connection conn = DBManage.getConnection();

  PreparedStatement pstmt = null;

  int count = 0;

  try

  {

  pstmt = conn.prepareStatement(DELETEATEACHER);

  pstmt.setInt(1, id);

  count = pstmt.executeUpdate();

  if (count != 0)

  {

  flag = true;

  }

  conn.commit();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  finally

  {

  if (null != conn)

  {

  try

  {

  conn.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  if (null != pstmt)

  {

  try

  {

  pstmt.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  }

  return flag;

  }

  

  // 修改一条记录

  public static boolean updateTeacher(int id, String name,

  String password)

  {

  boolean flag = false;

  Connection conn = DBManage.getConnection();

  PreparedStatement pstmt = null;

  int count = 0;

  try

  {

  pstmt = conn.prepareStatement(UPDATETEACHER);

  pstmt.setInt(1, id);

  pstmt.setString(2, password);

  pstmt.setString(3, name);

  count = pstmt.executeUpdate();

  if (count != 0)

  {

  flag = true;

  }

  conn.commit();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  finally

  {

  if (null != conn)

  {

  try

  {

  conn.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  if (null != pstmt)

  {

  try

  {

  pstmt.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  }

  return flag;

  }

  

  // 查询信息

  public static void seleteTeacher()

  {

  Connection conn = DBManage.getConnection();

  Statement stmt = null;

  ResultSet rs = null;

  try

  {

  stmt = conn.createStatement();

  rs = stmt.executeQuery(SELECTTEACHER);

  while (rs.next())

  {

  System.out.print(rs.getInt("teaId") + " ");

  System.out.print(rs.getString("name") + " ");

  System.out.println(rs.getString("password"));

  }

  conn.commit();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  finally

  {

  if (null != conn)

  {

  try

  {

  conn.close();

  }

  catch (SQLException e)

  {

  e.printStackTrace();

  }

  }

  }

  }

  

  

  // 测试

  public static void main(String[] args)

  {

  boolean flag = TeacherManage.insertTeacher("cc", "456");

  boolean flag = TeacherManage.deleteATEacher(42);

  boolean flag = TeacherManage.updateTeacher("haha", "147");

  System.out.println(flag);

  TeacherManage.seleteTeacher();

  }

  }

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册