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

我是一个初学者,对JSP的添加和删除都是弄不成!希望那位大哥或(大姐)能帮我写个源代码!谢谢了!... 我是一个初学者,对JSP的添加和删除都是弄不成!希望那位大哥或(大姐)能帮我写个源代码!谢谢了! 展开
 我来答
孩儿跟住爹
推荐于2017-09-14 · 超过10用户采纳过TA的回答
知道答主
回答量:39
采纳率:0%
帮助的人:25.9万
展开全部
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;

}

}
可以叫我表哥
推荐于2017-09-02 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464977
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
在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);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式