Fork me on GitHub

jdbc连接数据库的四个对象

DriverManager  驱动类     

       DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用

           原因有2个:

                        > 导致驱动被注册2次。

                        > 强烈依赖数据库的驱动jar

             解决办法:

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

 Connection  连接类

   static Connection getConnection(String url, String user, String password)

           试图建立到给定数据库 URL 的连接。

          getConnection("jdbc:mysql://localhost:3306/day06", "root", "root");

          URL:SUN公司与数据库厂商之间的一种协议。

                 jdbc:mysql://localhost:3306/day06

                协议 子协议  IP :端口号 数据库

                   数据库类型

                          mysql: jdbc:mysql://localhost:3306/day14 或者 jdbc:mysql:///day14(默认本机连接)

                          oracle: jdbc:oracle:thin:@localhost:1521:sid

            getConnection(String url, Properties info)

                  Properties info = new Properties();//要参考数据库文档  可以用文件代替

                  info.setProperty("user", "root");//用户名

                  info.setProperty("password","root");//密码

    //获取连接对象返回值connection

   Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day14?user=root&password=root");

Statement  操作数据库类

           //创建操作数据库对象

       Statement state=conn.createStatement();

     String sql="sql语句";

    Result rt= state.executeQuery(sql);//返回结果集

      问题:存在sql注入

      解决办法

         使用传入参数方式 防止sql注入
Statement (PreparedStatement)//预编译对象PreparedStatement

    特点:

           1.性能要高

           2.会把sql语句先编译

           3.sql语句中的参数会发生变化,过滤掉用户输入的关键字。

  Statement state= conn.preparedStatement();

       String sql="select * from user where username=? and password=?";

       Result rt= state.executeQuery(sql);//返回结果集

       state.setString(1,username);

       state.setString(2,password);

      执行对象返回的结果
         ResultSet executeQuery();
         int executeUpdate();
         boolean execute();

         delete from users where id=?
         ps.setInt(1,5);


ResultSet 结果集

  结果集(客户端存表数据的对象)

           //获取数据

            next();

            getString();
            getDouble();
            getDate();

总结
利用四个核心对象编写代码


try{
              //加载驱动
              Class.forName("com.mysql.jdbc.Driver");
              //创建连接Connection
              Connection conn = DriverManager.getConnection("jdbc:mysql:///day06","root","abc");
              //得到执行sql的statement对象
              //conn.createStatement();
              PreparedStatement ps = conn.prepareStatement("select * from users where name=? and pwd=?");
              ps.setString(1,"tom");
              ps.setString(2,"123");
              //执行语句,并返回结果
              ResultSet rs = ps.executeQuery();
              //处理结果
             while(rs.next()){
                  User u = new User();
                  u.setId(rs.getInt(1));
                     .... }
                       }

          catch(Exception e){
              e.printSt...  }

          finally{
            //关闭资源
            if(rs!=null)
            {rs.close();}
            if(ps!=null)
            {ps.close();}
            if(conn!=null)
            {conn.close();}
               }

posted on 2017-01-03 12:02  TopTime  阅读(354)  评论(2编辑  收藏  举报