package com.test; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException; /**
* jdbc使用预编译进行插入操作
*
* @Author: 张昊亮
* @Date: 2016年6月16日 上午10:56:43
*/
public class JdbcTest3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { // 注册驱动
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver"); // 获得链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhl", "root", "root"); // 得到操作数据库sql语句的预编译对象PraparementStatement
PreparedStatement ps = conn.prepareStatement("insert into user(id,usename,password) values(?,?,?)");
ps.setString(1, "5");
ps.setString(2, "xiaoming");
ps.setString(3, "xiaoming"); // 执行
int rs = ps.executeUpdate();
System.out.println(rs); // 释放资源
ps.close();
conn.close();
}
} 结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATUAAACRCAIAAAAth7gfAAAQ3UlEQVR4nO2d0W8Uxx3H569pHwqnNYhVeqoiVaoqRW0tYnyOUqKCTpUVWjWJABtHCRW8xLlERrZlwCLQhvISCLqQYK4JIkjENSfM+Xy33OVcu60qIqSqSMYmfsD0wX3Y3dnZ3+7e3nnnbmd2fh/9JJbd2b397m++t3N759+Qra2tNYZDhw6tCczT9afPn21uPnv2/PHjzeszmydPbr7++uahQ5ujo5ul+5vfP33+bPP58+fr6+stHvDJkyf1ev3GjRsXL1w4k3t/4vi7E8ffPftB7uPz52/cuPHtt99u4yTv3r07NjZ28uTJjz76qFarra6ubuMgCLK2tkbk8ieCJAzC4LMV/Ykg8UKd6bUo+hNBYgb9iSDi4ju+tVahPxEkXkxnsv507qjoTwSJF+/4Fv2JIKKA/kQQQWE/fLKfQvHzJ4KIDvoTQcQF/Ykg4hLoz98iCBI3zfz5MYKowZaohPiTyz3avARcDhUviRHSBBU0AuT25727X0aJNSbl9+5+uXH/HUkDCElkqKDRV3LcNgwk3J/zxZtRgk35fPHm9wsnJQ0gJJGhgkZfyX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