MySQL的总结

一、MySQL的连接

      1、使用之前记得要在工程的build path中添加 MySQL-connector-Java 的 jar 包

     2、使用DriverManager 连接数据库;

     3、使用Class.forName"(com.mysql.jdbc.Driver")加载驱动;

     4、登录连接数据库(三种方法)

     5、向数据库发送SQL语句,处理结果

      6、关闭数据库连接


 4、登录连接数据库(三种方法)

(1)String URL ="jdbc:mysql://localhost:3306/test?user=root&password=123";

                Connection conn = DriverManager.getConnection(URL);  

(2)String URL ="jdbc:mysql://localhost:3306/test";

                String user ="root";

                String password ="123";

                Connection conn = DriverManager.getConnection(URL, user, password);

(3)String URL ="jdbc:mysql://localhost:3306/test";

                Properties connectionProps =newProperties();

                connectionProps.put("user",this.userName);

                connectionProps.put("password",this.password);

                Connection conn = DriverManager.getConnection(URL, connectionProps);

 


 

 5、向数据库发送SQL语句(三种创建语句:Statement 、PreparedStatement 、CallableStatement )

  • Statement

Connection.createStatement() ——创建普通的语句,它通常不需要提供参数。

  • PreparedStatement

Connection.prepareStatement(String stringSQL)——创建预编译语句,通常要提供一个带有占位符的字符串 SQL 语句。

  • CallableStatement

Connection.prepareCall()——创建存储过程。

     

     Statement:

private static int addStudent2(student stu) throws Exception{

              Connection con = jdbcTest.getcon();

              String sql = "insert into t_student values ("+stu.getId()+"','"+stu.getStuName()+"','"+stu.getAge()+"',"+stu.getGradeName()+")";

              Statement stm = con.createStatement();

              int result=stm.executeUpdate(sql);

              jdbcTest.close(stm, con);

              return result;

}

 

 Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和execute。使用哪一个方法由 SQL 语句所产生的内容决定。

方法

执行语句

executeQuery

用于产生单个结果集的语句,例如 SELECT 语句

executeUpdate

执行 INSERT、UPDATE 或 DELETE 语句以及 SQLDDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE

execute

用于执行返回多个结果集、多个更新计数或二者组合的语句。

     

     PreparedStatement(Statement的子类)

插入数据

修改数据

public boolean Insert(User user){         

        boolean flag=true;

        Connection conn=null;

        PreparedStatement ps=null;    //创建PreparedStatement 对象

        String sql= "insert into user (name,pwd) values(?,?)";  //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。

        conn=JDBCTest.getConn();

        try {

            ps=conn.prepareStatement(sql);

            ps.setString(1, user.getName()); //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。

            ps.setString(2, user.getPwd());

            int i=ps.executeUpdate();

            if(i==0){

                flag=false;

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            JDBCTest.closeAll(null, ps, conn);

        }

        return flag;

 

    }

 

 

public boolean Update(User user){

        boolean flag=true;

        Connection conn=null;

        PreparedStatement ps=null;

        String sql="update user set pwd=? where name=?";

        conn=JDBCTest.getConn();

        try {

            ps=conn.prepareStatement(sql);

            ps.setString(1, user.getPwd());

            ps.setString(2, user.getName());

            int i= ps.executeUpdate();

            if(i==0){

                flag=false;

            }

 

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            JDBCTest.closeAll(null, ps, conn);

        }

        return flag;

    }

 

 

 删除数据

查看数据

 

public boolean Delete(int id){

        boolean flag=true;

        Connection conn=null;

        PreparedStatement ps=null;

        String sql="delete from user where id=?";

        conn=JDBCTest .getConn();

        try {

            ps=conn.prepareStatement(sql);

            ps.setInt(1, id);

            int i=ps.executeUpdate();

            if(i==0){

                flag=false;

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            JDBCTest.closel(ps, conn);

        }

 

        return flag;

    }

 

 

public List<User> Select(){

         List<User> list= new ArrayList<User>();

         Connection conn=null;

         PreparedStatement ps=null;

         ResultSet rs=null;

         String sql="select * from user";

         conn=JDBCTest .getConn();

         try {

              ps=conn.prepareStatement(sql);

             rs=ps.executeQuery();

             while(rs.next()){

                User user= new User();

                user.setName(rs.getString("name"));

                list.add(user);

             }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            JDBCTest.close(rs, ps, conn);

        }

 

         return list;

     }

 

继承了 Statement 接口中所有方法的 PreparedStatement 接口都有自己的executeQuery、executeUpdate 和 execute 方法。Statement 对象本身不包含 SQL语句,因而必须给 Statement.execute 方法提供 SQL 语句作为参数。PreparedStatement 对象并 不将SQL 语句作为参数提供给这些方法,因为它们已经包含预编译 SQL 语句。CallableStatement 对象继承这些方法的PreparedStatement 形式。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林零七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值