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

时间:2022-12-11 21:09:42
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();
        }
    }
阅读(954) | 评论(0) | 转发(0) |
0

上一篇:String[] 与 ArrayList 之间的转换

下一篇:Struts中使用数据库

相关热门文章 给主人留下些什么吧!~~ 评论热议