zoukankan      html  css  js  c++  java
  • java连接mysql数据库 三 实现增删改查操作

    同以前一样,先写一个数据库打开和关闭操作类

    public class DBConnection {
    
        String driver = "com.mysql.jdbc.Driver";
        String url= "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";
        
        public Connection conn;
    
        public DBConnection() {
    
            try {
                Class.forName(driver);// 加载驱动程序
                conn = (Connection) DriverManager.getConnection(url, user, password);// 连续数据库
                
                if(!conn.isClosed())
                    System.out.println("Succeeded connecting to the Database!"); 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public void close() {
            try {
                this.conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public class JDBCTest {
    
        public static void main(String[] args){
            //add(uname, uemail, upwd);
            //update("李诗诗","lishishi@com","666");
            //show();
            del("王小五");
        }
        //插入操作
        public static int add(String uname,String uemail,String upwd) {
            int i=0;
            String sql="insert into employee (name,email,pwd) values (?,?,?)";
            DBConnection db = new DBConnection();
            try {        
                PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
                preStmt.setString(1, uname);
                preStmt.setString(2, uemail);
                preStmt.setString(3, upwd);
                preStmt.executeUpdate();
                //Statement statement = (Statement) db.conn.createStatement();
                //statement.executeUpdate(sql);
                
                preStmt.close();
                db.close();//关闭连接 
            } catch (Exception e) {
                e.printStackTrace();
            }
            return i;//返回影响的行数,1为执行成功
        }
        //查找操作
        public static void show(){
             String sql ="select * from employee";
             DBConnection db = new DBConnection();
             
             System.out.println("-----------------");
             System.out.println("姓名" +"	"+ "邮箱" +"	"+ "日期");
             System.out.println("-----------------");
             
             try {
                Statement stmt = (Statement) db.conn.createStatement();
                ResultSet rs = (ResultSet) stmt.executeQuery(sql);
                while(rs.next()){
                    String uname = rs.getString("name");
                    String uemail = rs.getString("email");
                    String uhiredate = rs.getString("hiredate");
                    //可以将查找到的值写入类,然后返回相应的对象 
                    //这里 先用输出的端口显示一下
                    System.out.println(uname +"	"+ uemail +"	"+ uhiredate);
                }
                rs.close();
                db.close();//关闭连接 
            } catch (SQLException e) {
                e.printStackTrace();
            } 
        }
        //更新操作
        public static int update(String uname,String uemail,String upwd) {
            int i =0;
            String sql="update employee set email=?,pwd=? where name=?";
            DBConnection db = new DBConnection();
            
            try {
                PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
                preStmt.setString(1, uemail);
                preStmt.setString(2, upwd);
                preStmt.setString(3, uname);
                preStmt.executeUpdate();
                
                preStmt.close();
                db.close();//关闭连接 
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return i;//返回影响的行数,1为执行成功
        }
        //删除操作
        public static int del(String uname) {
            int i=0;
            String sql="delete from employee where name=?";
            DBConnection db = new DBConnection();
            try {    
                PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
                preStmt.setString(1, uname);
                preStmt.executeUpdate();
                
                preStmt.close();
                db.close();//关闭连接 
            } catch (SQLException e){
                e.printStackTrace();
            }
            return i;//返回影响的行数,1为执行成功
        }
    }
  • 相关阅读:
    CCF CSP 201509-1 数列分段
    CCF CSP 201503-1 图像旋转 (降维)
    CCF CSP 201412-1 门禁系统
    CCF CSP 201409-1 相邻数对
    CCF CSP 201403-1 相反数
    CCF CSP 201312-1 出现次数最多的数
    Win10环境下 HTTP 错误 500.19
    牛客网 整数拆分 (动态规划)
    牛客网 放苹果
    LeetCode9 回文数
  • 原文地址:https://www.cnblogs.com/liushao/p/6381678.html
Copyright © 2011-2022 走看看