PreparedStatement執行sql語句

Posted 微风星语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PreparedStatement執行sql語句相关的知识,希望对你有一定的参考价值。

 

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

import org.junit.Test;

import util.JdbcUtil;
/**
 * PreparedStatement執行sql語句
 * @author APPle
 *
 */
public class Demo1 {

    /**
     * 增加
     */
    @Test
    public void testInsert() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            //1.获取连接
            conn = JdbcUtil.getConnection();
            
            //2.准备预编译的sql
            String sql = "INSERT INTO student2(NAME,gender) VALUES(?,?)"; //?表示一个参数的占位符
            
            //3.执行预编译sql语句(检查语法)
            stmt = conn.prepareStatement(sql);
            
            //4.设置参数值
            /**
             * 参数一: 参数位置  从1开始
             */
            stmt.setString(1, "李四");
            stmt.setString(2, "男");
            
            //5.发送参数,执行sql
            int count = stmt.executeUpdate();
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(conn, stmt);
        }
    }
    
    /**
     * 修改
     */
    @Test
    public void testUpdate() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            //1.获取连接
            conn = JdbcUtil.getConnection();
            
            //2.准备预编译的sql
            String sql = "UPDATE student2 SET NAME=? WHERE id=?"; //?表示一个参数的占位符
            
            //3.执行预编译sql语句(检查语法)
            stmt = conn.prepareStatement(sql);
            
            //4.设置参数值
            /**
             * 参数一: 参数位置  从1开始
             */
            stmt.setString(1, "王五");
            stmt.setInt(2, 9);
            
            //5.发送参数,执行sql
            int count = stmt.executeUpdate();
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(conn, stmt);
        }
    }
    
    /**
     * 删除
     */
    @Test
    public void testDelete() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            //1.获取连接
            conn = JdbcUtil.getConnection();
            
            //2.准备预编译的sql
            String sql = "DELETE FROM student2 WHERE id=?"; //?表示一个参数的占位符
            
            //3.执行预编译sql语句(检查语法)
            stmt = conn.prepareStatement(sql);
            
            //4.设置参数值
            /**
             * 参数一: 参数位置  从1开始
             */
            stmt.setInt(1, 9);
            
            //5.发送参数,执行sql
            int count = stmt.executeUpdate();
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(conn, stmt);
        }
    }
    
    /**
     * 查询
     */
    @Test
    public void testQuery() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            //1.获取连接
            conn = JdbcUtil.getConnection();
            
            //2.准备预编译的sql
            String sql = "SELECT * FROM student2"; 
            
            //3.预编译
            stmt = conn.prepareStatement(sql);
            
            //4.执行sql
            rs = stmt.executeQuery();
            
            //5.遍历rs
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id+","+name+","+gender);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            JdbcUtil.close(conn,stmt,rs);
        }
    }
}

 

以上是关于PreparedStatement執行sql語句的主要内容,如果未能解决你的问题,请参考以下文章

PL/SQL Developer查詢已經執行過的SQL語句記錄 Ctrl+E

Mysql Programming CS 155P笔记 Dynamic SQL

实验11——java线程模拟卖票

SQL模擬死結產生

Linux 三大编辑神器 - sed (中集)

js條件結構和循環結構