增
package JDBC实现增删改;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//增
public class gai {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 加载驱动类 有异常声明处理一下即可
Class.forName("com.mysql.jdbc.Driver");
//构建字符串连接与相关信息
String utl = "jdbc:mysql://localhost:3306/test0616";
String user = "root";
String pw = "wlb123123";
// 让驱动管家提供一个 [连接对象] 给我们
Connection connection = DriverManager.getConnection(utl, user, pw);
// 输出连接对象 测试结果
System.out.println(connection);
//判断连接对象
if (connection == null){
System.out.println("连接失败");
}
else{
System.out.println("连接成功");
//获得执行者
Statement statement = connection.createStatement();
System.out.println("执行者生成:" + statement);
//构建sql语句
String sql = ("UPDATE usertable SET `1`='bbb' WHERE `2`='ccc';");
//让执行者干活
int i = statement.executeUpdate(sql);
System.out.println("受影响的行数是:" + i);
//判断结果
if (i >= 1){
System.out.println("添加成功");
}
else{
System.out.println("添加失败");
}
//关闭数据
System.out.println("关闭数据");
connection.close();
}
}
}
想删和改只需要把sql语句改一下就好了