案例:演示PreparedStatement对象的使用

创建Example02类,在该类中使用PreparedStatement对象来插入数据

public class Example02 {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		PreparedStatement  preStmt = null;
			try {
	             // 加载数据库驱动
				Class.forName("com.mysql.jdbc.Driver");
				String url = "jdbc:mysql://localhost:3306/jdbc";
				String username = "root";
				String password = "root";
	             // 创建应用程序与数据库连接的Connection对象
				conn = DriverManager.getConnection(url, username, password);
				// 执行的SQL语句
				String sql = "INSERT INTO users(name,password,email,birthday)"
						+ "VALUES(?,?,?,?)";
	             // 创建执行SQL语句的PreparedStatement 对象
				preStmt = conn.prepareStatement(sql);
				preStmt.setString(1, "zl");
				preStmt.setString(2, "123456");
				preStmt.setString(3, "[email protected]");
				preStmt.setString(4, "1789-12-23");
				preStmt.executeUpdate();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {    // 释放资源
				if (preStmt != null) {
					try {
						preStmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					preStmt = null;
				}
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					conn = null;
				}
			}
		}
	}

猜你喜欢

转载自blog.csdn.net/daqi1983/article/details/121594029
今日推荐