** 删**
package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建链接
Connection connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/zjgm?useSSL=true&characterEncoding=utf-8&user=root&password=123456”);
System.out.println(“创建链接成功”);
//3.写SQL
String sql=“delete from stu where id=1”;
//4.得到statement对象
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行SQL
statement.executeUpdate();
//6.处理结果集
/* while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
int age=resultSet.getInt(3);
System.out.println(id +name +age);
}*/
//7.关闭资源
connection.close();
statement.close();
//resultSet.close();
}
}
注释:以之前的最早的代码删除数据(第一个做的)
增
package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建链接
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true”);
System.out.println(“创建链接成功”);
//3.写SQL
String sql=" insert into stu set id=2,name=‘hhh’,age=18";
//4.得到statement对象
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行SQL
statement.executeUpdate();
//6.处理结果集
/* while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
int age=resultSet.getInt(3);
System.out.println(id +name +age);
}*/
//7.关闭资源
connection.close();
statement.close();
//resultSet.close();
}
}
第二条新增文件是为"改"准备的
改
package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建链接
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true”);
System.out.println(“创建链接成功”);
//3.写SQL
String sql=" update stu set age=831 where name=‘hhrh’";
//4.得到statement对象
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行SQL
statement.executeUpdate();
//6.处理结果集
/* while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
int age=resultSet.getInt(3);
System.out.println(id +name +age);
}*/
//7.关闭资源
//connection.close();
//statement.close();
//resultSet.close();
}
}
后面自己新增了一条数据,“改”在新数据上修改
查
鉴于新产生的数据以ID为条件查找
package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建链接
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true”);
System.out.println(“创建链接成功”);
//3.写SQL
String sql=“select id from stu”;
//4.得到statement对象
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行SQL
ResultSet resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
int id=resultSet.getInt(1);
System.out.println(id );
}
//7.关闭资源
connection.close();
statement.close();
resultSet.close();
}
}