在今天的学习里,学到了如何完成简单的增删查改操作,在这里撰写一篇文章以便自己整理回忆。
首先要定义一些字符串。
//数据库连接地址
private static final String URL = "Jdbc:mysql://localhost:3306/test?serverTimezone=GMT";
//用户名
private static final String USER_NAME = "root";
//密码
private static final String PASSWORD = "root";
这是一个对应数据表的bean类。
public class Test {
public Test(int id, int password) {
this.id = id;
this.password = password;
}
private int id;
private int password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPassword() {
return password;
}
public void setPassord(int passord) {
this.password = passord;
}
@Override
public String toString() {
return "Test{" +
"id=" + id +
", passord=" + password +
'}';
}
}
获得数据库连接,将其封装成函数,后续直接调用函数即可实现连接。
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
插入数据
public static void insert(Test test){
Connection connection = DBUtil.getConnection();
String sql="insert into test (id,password) values(?,?)";
try {
PreparedStatement prst =connection.prepareStatement(sql);
prst.setInt(1, test.getId());
prst.setInt(2, test.getPassword());
prst.executeUpdate();
prst.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
删除数据
public static void delete(int id){
Connection connection = DBUtil.getConnection();
String sql = "delete from test where id='" + id + "'";
try {
PreparedStatement prst =connection.prepareStatement(sql);
prst.executeUpdate();
prst.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
查询数据
public List findAll() {
List list = new ArrayList<>();
Connection connection = null;
PreparedStatement prst = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
String sql = "SELECT * FROM test";
prst = connection.prepareStatement(sql);
rs = prst.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
int password = rs.getInt(2);
Test test = new Test(id, password);
list.add(test);
}
System.out.println(list.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnetion(rs, prst, connection);
}
return list;
}
修改数据
public static void update(Test test){
Connection connection = DBUtil.getConnection();
String sql = "update students set password='" + test.getPassword() + "' where id='" + test.getId() + "'";
try {
PreparedStatement prst =connection.prepareStatement(sql);
prst.executeUpdate();
prst.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
关闭资源
public static void closeConnetion(ResultSet rs, PreparedStatement prst,Connection connection){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}if (prst != null) {
try {
prst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这样在主函数里调用各部分函数,即可完成简单的增删查改操作了!