java如何生成数据_java 只有源代码怎么生成数据库

其实,也就那几步而已:

别忘了,加载jar文件

加载驱动

获得Connection

获得PreparedStatement /PreparedStatement

执行sql语句

处理结果集ResultSet

关闭连接

mport java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import Entity.JIAJU;public class JiaJu { public JIAJU selectExe(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); // list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void insertJia(JIAJU jia) { try { Connection con = ConnectionManager.getConnection(); String sql = "insert into jiaju values(?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, jia.getShouhinName()); ps.setString(2, jia.getShouhinColor()); ps.setInt(3, jia.getShouhinPrice()); ps.setString(4, jia.getShouhinPai()); ps.setString(5, jia.getShouhinShi()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public List selectJia() { List list = new ArrayList(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju "; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { JIAJU jia = new JIAJU(); jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return list; } public JIAJU selectbuy(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void updateLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "update jiaju set shouhinPrice=? where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,jia.getShouhinPrice()); ps.setInt(2, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } } public void deleteLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "delete from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } }}

取消

评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值