此篇文章的基础来自https://blog.csdn.net/weixin_39038328/article/details/136197311?spm=1001.2014.3001.5501
一、 数据查询
public static void search() {try {Class.forName("com.mysql.jdbc.Driver"); // 1.加载驱动//2.建立连接Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuye?useUnicode=true&characterEncoding=utf-8", "root", "2020");//3.创建执行的SQL语句String sqlString = "select * from student"; //要想执行sql语句,必须获得Statement实例Statement statement = (Statement) connection.createStatement();//4.执行sql语句ResultSet re = (ResultSet) statement.executeQuery(sqlString);//5.处理结果while (re.next()) {String name = re.getString("name");String sno = re.getString("sno");String phone = re.getString("phone");System.out.println(name +" "+ sno +" " + phone);}//6.释放资源if(re!=null) {re.close();}if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("找不到驱动类,加载失败");e.printStackTrace();} }
二、 数据添加
public static void insert() {// 定义数据量int temp =0;try {Class.forName("com.mysql.jdbc.Driver"); // 1.加载驱动//2.建立连接Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuye?useUnicode=true&characterEncoding=utf-8", "root", "2020");//3.创建执行的SQL语句String insert = "INSERT into student (name,sno,phone) VALUES ('张三',20220152,'18331011889')";Statement statement = (Statement) connection.createStatement();//4.执行sql语句statement.executeUpdate(insert);if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("找不到驱动类,加载失败");e.printStackTrace();} }
三、数据修改
public static void update() {// 定义数据量int temp =0;try {Class.forName("com.mysql.jdbc.Driver"); // 1.加载驱动//2.建立连接Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuye?useUnicode=true&characterEncoding=utf-8", "root", "2020");//3.创建执行的SQL语句String update = "update student set name ='李四',sno = 2016423070 ,phone = '18331011889' where id = 32";Statement statement = (Statement) connection.createStatement();//4.执行sql语句statement.executeUpdate(update);if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("找不到驱动类,加载失败");e.printStackTrace();} }
四、数据删除
public static void delete() {// 定义数据量int temp =0;try {Class.forName("com.mysql.jdbc.Driver"); // 1.加载驱动//2.建立连接Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuye?useUnicode=true&characterEncoding=utf-8", "root", "2020");//3.创建执行的SQL语句String delete = "delete from student where id = 32";Statement statement = (Statement) connection.createStatement();//4.执行sql语句statement.executeUpdate(delete);if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("找不到驱动类,加载失败");e.printStackTrace();} }