PreparedStatement与Statement

Statement操作数据库:
增删改:使用executeUpdate();
查询:使用executeQuery();

ResultSet:保存结果集 select * from xxx
next():光标下移,判断是否有下一条数据;true/false
previous():  true/false
getXxx(字段名|位置):获取具体的字段值

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBCDemo {
    private final String URL="jdbc:mysql://localhost:3306/java?useSSL=false";
    private final String USERNAME = "root";
    private final String PASSWORD = "123456";
    public void update(){//实现增删改
        Connection connection = null;
        Statement stmt = null;
        try {
            //a.导入驱动
            Class.forName("com.mysql.jdbc.Driver");
            //b.与数据库建立连接
            connection =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //c.发送sql,执行增删改、查
            stmt = connection.createStatement();
            //String sql = "insert into book(name,price) values('小说',50)";
            //String sql = "update book set name='科学' where id=17";
            String sql = "delete from book where id=19";
            //d.执行sql语句
            stmt.executeUpdate(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
            if(stmt!=null)           stmt.close();
            if(connection!=null)    connection.close();
        }catch(SQLException e){
            e.printStackTrace();
            }
        }
    }
    public void query(){//实现查询
        Connection connection = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //a.导入驱动
            Class.forName("com.mysql.jdbc.Driver");
            //b.与数据库建立连接
            connection =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //c.发送sql,执行增删改、查
            stmt = connection.createStatement();
            String sql = "select * from book";
            //执行sql [增删改executeUpdate(),查询executeQuery()]
            rs = stmt.executeQuery(sql);
            //处理结果
            while(rs.next()){//判断下一行是否有数据
                String na = rs.getString("name");
                String pri = rs.getString("price");
                System.out.println(na+"--"+pri);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
                if(rs!=null)            rs.close();
                if(stmt!=null)           stmt.close();
                if(connection!=null)    connection.close();
        }catch(SQLException e){
            e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        JDBCDemo j = new JDBCDemo();
        //j.update();
        j.query();
    }
}

PreparedStatement操作数据库:

public interface PreparedStatement extends Statement
因此
增删改:executeUpdate()
查询:executeQuery();
--此外
赋值操作 setXxx();

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCPreparedStatement {
    private final String URL="jdbc:mysql://localhost:3306/java?useSSL=false";
    private final String USERNAME = "root";
    private final String PASSWORD = "123456";
    public void update(){//实现增删改
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            //a.导入驱动
            Class.forName("com.mysql.jdbc.Driver");
            //b.与数据库建立连接
            connection =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //c.发送sql,执行增删改、查
            String sql = "insert into book values(?,?,?)";
            pstmt = connection.prepareStatement(sql);
            
            pstmt.setString(1, "隔壁");
            pstmt.setString(2, "56");
            pstmt.setInt(3, 21);
            //String sql = "insert into book(name,price) values('小说',50)";
            //String sql = "update book set name='科学' where id=17";
            
            pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
            if(pstmt!=null)           pstmt.close();
            if(connection!=null)    connection.close();
        }catch(SQLException e){
            e.printStackTrace();
            }
        }
    }
    public void query(){
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //a.导入驱动
            Class.forName("com.mysql.jdbc.Driver");
            //b.与数据库建立连接
            connection =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //c.发送sql,执行增删改、查
            String sql = "select * from book where id=?";
            pstmt = connection.prepareStatement(sql);
            
            pstmt.setInt(1, 21);
            //执行sql [增删改executeUpdate(),查询executeQuery()]
            rs = pstmt.executeQuery();
            //处理结果
            while(rs.next()){//判断下一行是否有数据
                String na = rs.getString("name");
                String pri = rs.getString("price");
                System.out.println(na+"--"+pri);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
                if(rs!=null)            rs.close();
                if(pstmt!=null)           pstmt.close();
                if(connection!=null)    connection.close();
        }catch(SQLException e){
            e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        JDBCPreparedStatement j = new JDBCPreparedStatement();
        /*j.update();*/
        j.query();
    }
}

PreparedStatement与Statement在使用时的区别:

Statement:
1、sql定义

String sql =" insert into book(name,price) values('小说',50) " ;
2、stmt.executeUpdate(sql);

PreparedStatement:

1、sql定义

String sql =" insert into book(name,price) values(?,?,?) " ;
pstmt = connection.prepareStatement(sql);//预编译SQL
pstmt.setString(1, "隔壁");

pstmt.setString(2, "56");
pstmt.setInt(3, 21);

扫描二维码关注公众号,回复: 9898232 查看本文章

2、 pstmt.executeUpdate();

猜你喜欢

转载自www.cnblogs.com/hsy-go/p/12511734.html