java mysql pstm.set_求大师求师傅怎么使用java链接数据库

e64cc898dcc8889a978cc00f5b37427b.png

Super_Scholar

你好,初始阶段学习,用jdbc即可。以下是比较通用的java连接mysql,参考下:public class DBUtil {Connection conn;PreparedStatement pstm;ResultSet rs; public DBUtil(){try {Properties properties = new Properties();InputStream is = DBUtil.class.getResourceAsStream("../../../jdbc.properties");properties.load(is);Class.forName(properties.getProperty("driver"));conn = DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("uname"),properties.getProperty("upwd"));System.out.println("Ok~数据库成功连接!");} catch (Exception e) {System.out.println(e);}}//查public List find(String sql,Object...obj){Listlist = new ArrayList();try {pstm=conn.prepareStatement(sql);for (int i = 0; i < obj.length; i++) {pstm.setString(i+1, obj[i].toString());}rs = pstm.executeQuery();while(rs.next()){Mapmap= new HashedMap();ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();for (int i = 0; i < rsmd.getColumnCount(); i++) {map.put(rsmd.getColumnName(i+1), rs.getString(i+1));}list.add(map);}} catch (SQLException e) {e.printStackTrace();System.out.println(e);}//System.out.println(list);return list;}//改public int cud(String cudSql,Object...obj){try {pstm=conn.prepareStatement(cudSql);for(int i=0; i0){System.out.println("添加成功");}else{System.out.println("添加失败");}}*/}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值