jSP如何对SQL数据库进行添加和删除!

2024-11-23 21:56:34
推荐回答(2个)
回答1:

public class DBConnection {
private static final String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=Shop";
private static final String user="sa";
private static final String password="sa";
public static Connection conn=null;
public static Connection getConnection()
{
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
}
catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection con)
{
try {
if(con!=null)
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void closeStatement(Statement sm)
{
try {
if(sm!=null)
sm.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void closeResultSet(ResultSet rs)
{
try {
if(rs!=null)
rs.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void closePreparedStatement(PreparedStatement pstmt)
{
try {
if (pstmt != null)
pstmt.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class DBGoods {
public DBGoods() {
}

private Connection con = null;
private Statement sm = null;
private ResultSet rs = null;
public ArrayList queryGoods(String sql) {
ArrayList al = new ArrayList();
try {
con = DBConnection.getConnection();
sm = con.createStatement();
rs = sm.executeQuery(sql);
while (rs.next()) {
shop goods1 = new shop();
goods1.setID(rs.getLong("ID"));
goods1.setShopName(rs.getString("shopName"));
goods1.setClassName(rs.getString("className"));
goods1.setPublish(rs.getString("pubish"));
goods1.setPrice(rs.getFloat("price"));
goods1.setAmount(rs.getInt("Amount"));
al.add(goods1);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(sm);
DBConnection.closeConnection(con);
}
return al;

}

public void modigoods(String sql) {
try {
con = DBConnection.getConnection();
sm = con.createStatement();
sm.execute(sql);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(sm);
DBConnection.closeConnection(con);
}
}

public void insertgoods(String sql) {
try {
con = DBConnection.getConnection();
sm = con.createStatement();
sm.execute(sql);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(sm);
DBConnection.closeConnection(con);
}
}
public ArrayList queryorders(String sql) {
ArrayList al = new ArrayList();
try {
con = DBConnection.getConnection();
sm = con.createStatement();
rs = sm.executeQuery(sql);
while (rs.next()) {
order order = new order();
order.setOrderID(rs.getString("orderId"));
order.setSubmitTime(rs.getString("submitTime"));
order.setTotalPrice(rs.getFloat("TotalPrice"));
order.setIpAddress(rs.getString("IPAddress"));
order.setIsPayoff(rs.getBoolean("ispayoff"));
order.setIsSales(rs.getBoolean("isSales"));
al.add(order);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(sm);
DBConnection.closeConnection(con);
}
return al;

}
public ArrayList queryAllorders(String sql) {
ArrayList al = new ArrayList();
try {
con = DBConnection.getConnection();
sm = con.createStatement();
rs = sm.executeQuery(sql);
while (rs.next()) {
allorder order = new allorder();
order.setOrderID(rs.getString("orderID"));
order.setShopNO(rs.getInt("shopNO"));
order.setAmount(rs.getInt("Amount"));
al.add(order);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(sm);
DBConnection.closeConnection(con);
}
return al;

}

}

回答2:

在jsp中插入sql server中的数据
String condition="insert into teacher_job values"+"("+no+","+"'"+zkzh+"'"+","+"'"+xm+"'"+","+"'"+sfzh+"'"+","+"'"+syx+"'"+","+"'"+fwx+"'"+","+zcj+","+yycj+")";
sql.executeUpdate(condition);
在jsp中删除sql server中的数据
String condition="delete from teacher_job where no="+no;
sql.executeUpdate(condition);