jdbc连接

jdbc连接


第一步:数据厂商提供的连接驱动包


第二步:java代码连接数据库,实现数据操作(mysql为例)


       1.加载驱动
          Class.forName("com.mysql.jdbc.Driver");   


       2.创建连接


         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/person", "root", "123456"); 


          数据库连接信息
         url:jdbc:mysql://localhost:3306/database
         user:root
         password:安装时的密码


       3.创建可执行语句


          PreparedStatement pst = con.prepareStatement("sql语句");




        4.执行语句


            1>查询
              取得结果集  ResultSet rst = pst.executeQuery();
            
            2>添加和更新


               传参数值使用pst.setString(位置, 值);
               pst.execute(); 或 pst.executeUpdate();

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/shipeng22022/p/4614174.html