Java代码实现对数据库的增删查改

36 篇文章 0 订阅
package com.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class mysqlUtil {
	public static void main(String[] args) {
		List< Map<String,String> > maplist = new ArrayList<>();
		String sqlget = "select * from books";
		String[] params = { "bookId" ,"bookname","booktype","bookauthor"};
		maplist = show(sqlget, params);
		for(int i = 0; i < maplist.size(); i++) {
			String outstr = "id:" + maplist.get(i).get("bookId") + "||bookname:" + maplist.get(i).get("bookname");
			System.out.println(outstr);
		}
	}
	public static int add(String sql) {
        int i=0;
        DBConnection db = new DBConnection();
        try {        
            PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
            preStmt.executeUpdate();
            //Statement statement = (Statement) db.conn.createStatement();
            //statement.executeUpdate(sql);
            preStmt.close();
            db.close();//关闭连接 
            i = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;//返回影响的行数,1为执行成�?
    }
    public static void show(){
         String sql ="select * from books";
         DBConnection db = new DBConnection();
         System.out.println("-----------------");
         System.out.println("大家看一下我的图书");
         System.out.println("-----------------");
         try {
            Statement stmt = (Statement) db.conn.createStatement();
            ResultSet rs = (ResultSet) stmt.executeQuery(sql);
            while(rs.next()){
               
            	  String bookId = rs.getString("bookId");
            	String bookname = rs.getString("bookname");
                String booktype = rs.getString("booktype");
                String bookauthor = rs.getString("bookauthor");
                System.out.println(bookId +"\t"+ bookname +"\t"+ booktype+"\t"+bookauthor);
            }
            rs.close();
            db.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } 
    }
    public static int update(String sql) {
        int i =0;
        DBConnection db = new DBConnection();
        try {
            PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
            preStmt.executeUpdate();
            preStmt.close();
            i = 1;
            db.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    public static List< Map<String,String> > show(String sql, String[] params){
        // String sql ="select * from employee";
    	List< Map<String,String> > listmap = new ArrayList<>();
         DBConnection db = new DBConnection();
         ResultSet rs = null;
         try {
            Statement stmt = (Statement) db.conn.createStatement();
            rs = (ResultSet) stmt.executeQuery(sql);
            while(rs.next()){
            	Map<String,String> map = new HashMap<String,String>();
            	for(int i = 0; i < params.length; i++) {
            		
            		map.put(params[i], rs.getString(params[i]));
            	}
            	listmap.add(map);
            }
            rs.close();
            db.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
		return listmap; 
    }
    public static int del(String delstr) {
        int i=0;
        DBConnection db = new DBConnection();
        try {    
            PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(delstr);
            preStmt.executeUpdate();
            
            preStmt.close();
            db.close();
            i = 1;
        } catch (SQLException e){
            e.printStackTrace();
        }
        return i;
    }

}
	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值