用JAVA对数据库信息进行增加、删除、修改、查看怎么写谁能帮忙,能的加QQ1505209873我把我写好的代码发给

2024-11-30 17:34:07
推荐回答(4个)
回答1:

这个不是简单的数据库 数据的添加 删除 修改 和查看吗
这个是一个家具买卖页面所涉及的 曾删改查都有了,详细内容看代码
package Dao;

import 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();
}
}

}

回答2:

1.Connection
2.预处理
3.resultset
4.你想干啥干啥
5.顺序释放资源。。。你也可以不释放。。。。
建议:写一个类统一管理

回答3:

Hibernate,
多好的东西, 透明持久化, 侵入性小, 轻量.

回答4:

问题解决了吗?如果有需要,你可以直接找我