# Prepared Statements 和 Connector/J: 示例

__*useServerPrepStmts*__

+ JDBC 参数: `useServerPrepStmts=true`

```
// Do something in the connection
String sqlDropTable = "DROP TABLE IF EXISTS test.t1";
String sqlCreateTable = "CREATE TABLE test.t1 (id int primary key, name char(30))";
String sqlInsertIntoTable = "INSERT INTO test.t1 (id, name) VALUES (?, ?)";
~!@PreparedStatement@!~[] pss = new PreparedStatement[]{
    ~!@connection.prepareStatement@!~(sqlDropTable),
    ~!@connection.prepareStatement@!~(sqlCreateTable),
    ~!@connection.prepareStatement@!~(sqlInsertIntoTable)
};
pss[2].setInt(1, 10);
pss[2].setString(2, "ABC");
for (PreparedStatement ps:pss){
    ps.~!@executeUpdate@!~();
    ps.close();
}
// Finish
```
---------------------------------------------------------

```
Class demo scripts:

For MacOS and Linux: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/10-demo-jdbc-prepared-statement-01-show.sh

For Windows: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/10-demo-jdbc-prepared-statement-01-show.bat
```

以上脚本还能演示 JDBC 参数 cachePrepStmts 的功能, 在 Java 演示代码里找到 cachePrepStmts 并把它修改为 true, 然后重新运行脚本观察不同点。
