import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

//预处理方式

public class preperSTest {

 

         public static void main(String[] args) throws SQLException {

                   update("双鱼座", "《菜根谭》");

         }

 

         private static void update (String name, String book) throws SQLException {

                   Connection conn = null;

                   PreparedStatement ps = null;

                   ResultSet rs = null;

 

                   try {

                            // 创建连接

                            conn = JdbcUtils.getConnection();

 

                            String sql = " update student set name = ?,book = ? where id = 2";

 

                            ps = conn.prepareStatement(sql);

                            // 代替 占位符

                            ps.setString(1, name);

                            ps.setString(2, book);

 

                            int i = ps.executeUpdate();

                            System.out.println(i);

 

                  

                   } finally {

                            JdbcUtils.release(rs, ps, conn);

                   }

 

         }

}

 

修改前:

Jdbc操作数据库  改_占位符

修改后:

 

Jdbc操作数据库  改_占位符_02