jdbc-增删改查和封装增删改查
##第一个类:
public class JdbcUtils {
private static final String url=“jdbc:mysql://localhost:3306/huang”;
private static final String user=“root”;
private static final String password="";
private static final String driver=“com.mysql.jdbc.Driver”;
private static Connection con=null;
static{
try {
System.out.println(“正在加载mysql”);
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConection(){
try {
con= DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static void close(Connection con, Statement stmt, ResultSet rs){
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(stmt != null){
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con != null){
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
##第二个类:
public class JdbcDemo {
//插入数据(增删改)
public void create(int id,String username,String usersex,int userage){
try {
Connection con=JdbcUtils.getConection();
String sql=“insert into one(id,name,sex,age)value(?,?,?,?)”;
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.setString(2,username);
pstmt.setString(3,usersex);
pstmt.setInt(4,userage);
int row=pstmt.executeUpdate();
if(row > 0){
System.out.println(“插入成功”);
}else{
System.out.println(“插入失败”);
}
JdbcUtils.close(con,pstmt,null);
} catch (SQLException e) {
e.printStackTrace();
}
}
//删除
public void deleteLog(int id){
try {
Connection con=JdbcUtils.getConection();
String sql=“delete from one where id=?”;
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1,id);
int row=pstmt.executeUpdate();
if(row > 0){
System.out.println(“删除成功”);
}else{
System.out.println(“删除失败”);
}
JdbcUtils.close(con,pstmt,null);
} catch (SQLException e) {
e.printStackTrace();
}
}
//修改
public void updateLog(String username,int userid){
try {
Connection con=JdbcUtils.getConection();
String sql=“update one set name=? where id=?”;
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setInt(2,userid);
int row=pstmt.executeUpdate();
if(row >0 ){
System.out.println(“修改成功”);
}else{
System.out.println(“修改失败”);
}
JdbcUtils.close(con,pstmt,null);
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询
public void query(String userName){
try {
Connection con=JdbcUtils.getConection();
String sql=“select * from one where name=?”;
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,userName);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
int userId=rs.getInt(“id”);
String username=rs.getString(“name”);
String usersex=rs.getString(“sex”);
String userage=rs.getString(“age”);
System.out.println(userId +"="+ username +"="+ usersex +"==="+userage);
}
JdbcUtils.close(con,pstmt,rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
第三个类:
public class Main {
public static void main(String[] args){
JdbcDemo demo=new JdbcDemo();
//demo.create(5,"aaa2","bbb",33);
//demo.query("lrj");
//demo.deleteLog(4);
demo.updateLog("huang",3);
}
}
源码下载:https://download.csdn.net/download/weixin_44095165/15452577