20201103巴本欠才让

添加:INSERT INTO 表名(字段1,字段2)VALUES("值1“,”值2“)
删除:DELETE FROM 表名 WHERE 字段名称=”值“
修改:UPDATE 表名 SET 字段名称1=“值1”,字段名称1=“值1”WHERE 字段名称=“值”
package com.itlaobing.dbhelper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DBHelper {
/用main测试/
public static void main(String[] args) {
try {
save();
} catch (Exception e) {
e.printStackTrace();
}
}
/定义添加用户的方法/
public static void save() throws Exception {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、创建数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_user","root","");
//3、拼写sql语句
String sql = "INSERT INTO t_user (userName,passWord,gender)VALUES('宫本武藏','rb123','女')";
//4、发送sql
PreparedStatement prepareStatement = connection.prepareStatement(sql);
//5、执行sql
int i = prepareStatement.executeUpdate();
//6、处理结果
if(i > 0) {
System.out.println("添加成功");
}
//7、关闭资源
prepareStatement.close();
connection.close();
}
}

标签

评论

this is is footer