**jdbc数据库增删改查**
package com.company;
import util.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class add {
public static void main(String[] args) throws SQLException {
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
String sql="insert into tb_user(username,password) value ('qwer','14789')";
statement=connection.prepareStatement(sql);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DButil.closeAll(null,statement,connection);
}
}
}
package com.company;
import util.DButil;
import java.sql.*;
public class del {
public static void main(String[] args) throws SQLException{
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
String sql="delete from tb_user where username='pdd'";
statement=connection.prepareStatement(sql);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DButil.closeAll(null,statement,connection);
}
}
}
package com.company;
import util.DButil;
import java.sql.*;
public class Find {
public static void main(String[] args) throws SQLException {
Connection connection= null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection = DButil.getConnection();
String sql="select * from tb_user";
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DButil.closeAll(resultSet,statement,connection);
}
}
}
package com.company;
import util.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class update {
public static void main(String[] args) throws SQLException {
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DButil.getConnection();
String sql="update tb_user set password='123223'where id =3";
statement=connection.prepareStatement(sql);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
DButil.closeAll(null,statement,connection);
}
}
}
package util;
import java.sql.*;
public class DButil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/wzsxy","root","123456");
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
if (resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/93d7305a07aa808a1ea8b6a2aa9cb0ac.png)