jdbc连MySql增删改查和封装增删改查

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值