jdbc的增删改查(进阶版)
ps:前面的步骤可以参考前篇(java连接数据库和jdbc的增删改查操作)
简化代码
新建一个Java文件导入架包并创建两个新的类,将jdbc连接7步骤中的- - -1.建立驱动- - -2.创建连接- - -3.关闭资源- - -放到里面
package xy.zjgm;
import java.sql.*;//导入架包
public class Test2 {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
return connection;
}
public static void CloseAll(ResultSet resultSet,Statement statement,Connection connection) throws SQLException {
resultSet.close();
statement.close();
connection.close();
}
}
因为查询操作多一个结果集‘ResultSet’类,所以要对resultSet添加if语句使其可以兼容增删改查四种操作
public static void CloseAll(ResultSet resultSet,Statement statement,Connection connection) throws SQLException {
if (resultSet != null){
resultSet.close();
}
statement.close();
connection.close();
}
}
创建完后可以来进行增删查改时引用类来简化代码
增加
因为增加数据操作没有ResultSet类,所以在引用ClassAll类时在ResultSet中填null(结果不进行演示)
package xy.zjgm;
import java.sql.*;
public class Test3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=Test2.getConnection();
String sql="INSERT INTO student (Name,Age) VALUES('Nacy',19)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
Test2.CloseAll(null,statement,connection);
}
}
删除
package xy.zjgm;
import java.sql.*;
public class Test3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=Test2.getConnection();
String sql = "DELETE FROM student WHERE student.Name = 'Nacy'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
Test2.CloseAll(null,statement,connection);
}
}
更改
package xy.zjgm;
import java.sql.*;
public class Test3{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=Test2.getConnection();
String sql = "UPDATE student SET student.Age = '89' where Name='Jack'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
Test2.CloseAll(null,statement,connection);
}
}
查询
查询操作有结果集,所以在CloseAll中全要填写
package xy.zjgm;
import java.sql.*;
public class Test3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=Test2.getConnection();
String sql="select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int ID=resultSet.getInt(1);
String Name=resultSet.getString(2);
int Age=resultSet.getInt(3);
System.out.println("id:"+ID+" name:"+Name+" age:"+Age);
}
Test2.CloseAll(null,statement,connection);
}
}
关于static关键字
static静态关键字,它的存在使我们在调用对象是不用重新去new一个对象。
以此代码为例当public static Connection getConnection()
去掉static时我们在调用时就得使用Test2 test2=new Test2(); Connection connection=test2.getConnection();
来调用方法。它可以修饰成员变量,成员方法,不能修饰类,不能修饰构造方法 并且被修饰的变量,方法可以通过类名进行调用