JDBC preparedStatemnet usage parameter settings

Example 1: preparedStatement.setInt (1,1); a first parameter of type int, a value of 1.

Example 2: preparedStatement.setObject (1, "1"); a first parameter of type Object, a value of "1."

import java.sql.Connection;

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
* 测试PreparedStatement的基本用法
*
* @author Administrator
*/
public class Demo003 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/testjdbc", "root", "");

SQL = String "INSERT INTO t_user (the above mentioned id, username, pwd) values (,,???)";
PreparedStatement PS = conn.prepareStatement (SQL);
// ps.setInt (1, 9); // parameter index is 1 is calculated from the start, rather than 0
// ps.setString (2, "green");
// ps.setInt (. 3, 889);

// processing parameters may be used a method setObject
ps.setObject (. 1, 10);
ps.setObject (2, "blue");
ps.setObject (. 3, 396);
ps.execute ();
} the catch (Exception E) {
e.printStackTrace ();
}
}
}

Common Statement Method

execute (): Run the statement to return if there is a result set.

executeQuery (): Run select, return ResultSet result set.

executeUpdate (): run insert, update, delete operation returns the number of rows updated.

Editor:

https://www.cnblogs.com/qhcyp/p/10452573.html

import java.sql.Connection;

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
* 测试PreparedStatement的基本用法
*
* @author Administrator
*/
public class Demo003 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/testjdbc", "root", "");

SQL = String "INSERT INTO t_user (the above mentioned id, username, pwd) values (,,???)";
PreparedStatement PS = conn.prepareStatement (SQL);
// ps.setInt (1, 9); // parameter index is 1 is calculated from the start, rather than 0
// ps.setString (2, "green");
// ps.setInt (. 3, 889);

// 可以使用setObject方法处理参数
ps.setObject(1, 10);
ps.setObject(2, "蓝色");
ps.setObject(3, 396);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}

常用Statement方法

execute():运行语句,返回是否有结果集。

executeQuery():运行select,返回ResultSet结果集。

executeUpdate():运行insert,update,delete操作返回更新的行数。

转自:

https://www.cnblogs.com/qhcyp/p/10452573.html

Guess you like

Origin www.cnblogs.com/jndx-ShawnXie/p/11758667.html