91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

jdbc增刪改查代碼怎么寫

小億
84
2024-04-09 16:48:08
欄目: 編程語言

下面是一個簡單的示例代碼,演示如何使用JDBC進行增刪改查操作:

1、增加數據:

```java

Connection conn = DriverManager.getConnection(url, username, password);

String sql = "INSERT INTO table_name (column1, column2) VALUES (?, ?)";

PreparedStatement stmt = conn.prepareStatement(sql);

stmt.setString(1, "value1");

stmt.setString(2, "value2");

stmt.executeUpdate();

conn.close();

```

2、刪除數據:

```java

Connection conn = DriverManager.getConnection(url, username, password);

String sql = "DELETE FROM table_name WHERE column1 = ?";

PreparedStatement stmt = conn.prepareStatement(sql);

stmt.setString(1, "value1");

stmt.executeUpdate();

conn.close();

```

3、更新數據:

```java

Connection conn = DriverManager.getConnection(url, username, password);

String sql = "UPDATE table_name SET column1 = ? WHERE column2 = ?";

PreparedStatement stmt = conn.prepareStatement(sql);

stmt.setString(1, "new_value");

stmt.setString(2, "value2");

stmt.executeUpdate();

conn.close();

```

4、查詢數據:

```java

Connection conn = DriverManager.getConnection(url, username, password);

String sql = "SELECT * FROM table_name WHERE column1 = ?";

PreparedStatement stmt = conn.prepareStatement(sql);

stmt.setString(1, "value1");

ResultSet rs = stmt.executeQuery();

while (rs.next()) {

System.out.println(rs.getString("column1"));

System.out.println(rs.getString("column2"));

}

conn.close();

```

請注意,以上示例代碼是基于JDBC API,需要導入相應的包,并且需要替換相應的url、username、password、table_name、column和value等信息。

0
稷山县| 邮箱| 靖州| 南阳市| 瓮安县| 河池市| 宾阳县| 长兴县| 酉阳| 仁布县| 临邑县| 吉木乃县| 中山市| 平泉县| 沅陵县| 那曲县| 铁岭市| 新建县| 昭平县| 德格县| 高陵县| 塔城市| 延庆县| 彩票| 吉木乃县| 黄骅市| 巴里| 福安市| 三明市| 安图县| 林西县| 丰台区| 龙胜| 阳山县| 黎城县| 家居| 兴业县| 连云港市| 伊金霍洛旗| 隆德县| 寻乌县|