private void executeUpdate(String sql) { try { Class.forName(driver); Connection con = DriverManager.getConnection(url); Statement cmd = con.createStatement(); cmd.executeUpdate(sql); con.close(); } catch (Exception ex) { ex.printStackTrace(); } }
时间: 2024-04-03 20:34:57 浏览: 28
这是一个Java方法,用于执行数据库更新操作。该方法接受一个SQL语句作为参数,然后使用JDBC连接到数据库并执行该语句。具体来说,它会加载指定的数据库驱动程序(使用Class.forName方法),然后使用DriverManager.getConnection方法连接到指定的数据库,创建一个Statement对象来执行SQL语句,最后关闭连接。请注意,该方法没有对SQL语句的有效性进行检查,因此需要确保传递给它的SQL语句是正确的。
相关问题
详细注释代码 public void del_cource (String id) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/db_stu_score", "root", ""); String sql = "DELETE from tb_cource where id = ?; "; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, id); statement.executeUpdate(); statement.close(); connection.close(); }
这段代码是一个删除数据的方法,用于从数据库中删除指定id的课程记录。下面是对代码的详细注释:
```java
public void del_cource(String id) throws SQLException, ClassNotFoundException {
// 加载MySQL数据库驱动,这一步是必须的
Class.forName("com.mysql.jdbc.Driver");
// 连接到数据库
Connection connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/db_stu_score", "root", "");
// SQL语句,用于删除指定id的课程记录
String sql = "DELETE from tb_cource where id = ?; ";
// 创建PreparedStatement对象,用于执行SQL语句
PreparedStatement statement = connection.prepareStatement(sql);
// 设置SQL语句中的参数,这里是id
statement.setString(1, id);
// 执行SQL语句
statement.executeUpdate();
// 关闭PreparedStatement对象
statement.close();
// 关闭数据库连接
connection.close();
}
```
这段代码中的主要步骤包括:
1. 加载MySQL数据库驱动;
2. 连接到数据库(这里的`host`是数据库的主机名,`db_stu_score`是数据库名,`root`是数据库用户名,空字符串是密码,如果有密码需要填写具体密码);
3. 构建SQL语句,用于删除指定id的课程记录;
4. 创建PreparedStatement对象,用于执行SQL语句;
5. 设置SQL语句中的参数,这里是id;
6. 执行SQL语句;
7. 关闭PreparedStatement对象;
8. 关闭数据库连接。
package shixun7_1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class shixun7_1 { public static void main(String[] args) { String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/testdb?useSSL=false"; String userName ="root"; String userPwd ="123456"; try { Class.forName(driverName); Connection con = DriverManager.getConnection(url,userName,userPwd); //该对象用于与数据库建立连接 Statement s=con.createStatement(); //该对象可用于执行SQL语句 String query="create table student(id char(8),name char(8),score integer)"; //用于创建表格 s.executeUpdate(query); System.out.println("连接数据库成功"); } catch (Exception e) { e.printStackTrace(); System.out.print("连接失败"); } } }逐行翻译一下代码并解释
这段代码是一个 Java 程序,用于连接 MySQL 数据库并创建一个名为 student 的表格。下面是逐行翻译和解释:
```
package shixun7_1;
```
这是一个声明了包名的语句,表明这个类属于 shixun7_1 这个包。
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
```
这是导入了三个 Java 类,用于连接 MySQL 数据库和执行 SQL 语句。
```
public class shixun7_1 {
```
这是一个公共类,类名与文件名相同,用于定义程序的主类。
```
public static void main(String[] args) {
```
这是程序的主方法,从这里开始执行。
```
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/testdb?useSSL=false";
String userName ="root";
String userPwd ="123456";
```
这是定义了连接数据库需要的四个字符串,其中 driverName 是 MySQL 数据库的驱动程序类名,url 是连接数据库的 URL,userName 和 userPwd 是数据库的用户名和密码。
```
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url,userName,userPwd);
Statement s=con.createStatement();
String query="create table student(id char(8),name char(8),score integer)";
s.executeUpdate(query);
System.out.println("连接数据库成功");
} catch (Exception e) {
e.printStackTrace();
System.out.print("连接失败");
}
```
这是程序的主体,其中 try-catch 语句用于捕获可能出现的异常。在 try 语句块中,首先用 Class.forName() 方法加载 MySQL 数据库的驱动程序类,然后用 DriverManager.getConnection() 方法连接数据库,获取一个 Connection 对象。接着用 Connection.createStatement() 方法创建一个 Statement 对象,用于执行 SQL 语句。然后定义了一个字符串 query,用于创建一个名为 student 的表格。最后用 Statement.executeUpdate() 方法执行 SQL 语句,创建表格。如果连接数据库成功,程序会输出一条信息。如果出现异常,程序会输出错误信息。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<% //连接数据库 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM vote"); while(rs.next()){ String title = rs.getString("title"); %> <input type="radio" name="option" value="<%=title%>"><%=title%>
<% } } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> 提交投票选中后数据库数据变化
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)