美文网首页
jdbc基础

jdbc基础

作者: 岁月是首歌 | 来源:发表于2017-04-17 19:32 被阅读0次

    JDBC概念

    java连接数据库的一系列接口,接口的实现类交给第三方数据库厂商去实现

    四大参数

    驱动 com.mysql.jdbc.Driver

    DriverManager

    Class.forname("com.mysql.jdbc.Driver");
    

    Connection

    String url="jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=utf-8";
    String user="root";
    String password ="";
    Connection con = DriverManager.getConnection(url,user,password);
    

    Statement

            Statement statement=con.createStatement();
            String sql="update employee set username='YYT'where id=1 ";
            int i = statement.executeUpdate(sql);
            System.out.println(i);
            con.close();
    

    ResultSet

    //得到一条记录
    String sql="select * from employee ";
     ResultSet  rs =statement.executeQuery(sql);
     if(rs.next()){
         rs.getInt("字段名");
         String empno = rs.getString("empno");
     }
    
    //得到所有记录
            while(rs.next()){
                    int id = rs.getInt("id");
                    String empno = rs.getString("empno");
            }
    

    事务

    1.完整性
    2.一致性
    3.原子性
    4.隔离性

           try{
                con.setAutoCommit(false);
               String sql="delete from employee where id=1";
               String sql1="delete from employee where id=2";
               statement.executeUpdate(sql);
                statement.executeUpdate(sql1);
                con.commit();
            }catch(Exception e){
            System.out.println("操作失败");
                con.rollback();
            }
    

    简单的封装登录方法

    static String url="jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8";
        static String us="root";
        static String psw="";
        public void driver(){
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
    
                e.printStackTrace();
            }
        }
       public static Connection connect(){
           try {
              Connection con=DriverManager.getConnection(url,us,psw);
              return con;
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
               return null;
       }
     
       public User  login(String usn,String psw){
           Statement statement=null;
           Connection con=null;
           try {
             con = connect();
             statement = con.createStatement();
            String sql="select * from user where username='"+usn+"'and password="+psw;
            ResultSet qs = statement.executeQuery(sql);
            if(qs.next()){
                String username = qs.getString("username");
                String password = qs.getString("password");
                User user=new User(username,password);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            
        }   
         finally{
             try {
                if(statement!=null){
                    statement.close();
                }
                if(con!=null){
                con.close();
                }
            } catch (SQLException e) {
        
                e.printStackTrace();
            }
            
         }
            return null; 
           
       }
    
    
    

    相关文章

      网友评论

          本文标题:jdbc基础

          本文链接:https://www.haomeiwen.com/subject/gtqxzttx.html