📄 shopcartdao.java
字号:
package com.dongfang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.dongfang.vo.ShopCart;
public class ShopCartDAO {
public int getNextId()
{
int myID = 0;
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
conn = Tools.getConn();
try {
stm = conn.createStatement();
rs = stm.executeQuery("select max(id) id from shopcart");
if(rs.next())
{
myID = rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(stm!=null)
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ++myID;
}
public boolean saveShopCart(ShopCart shopCart)
{
boolean isSaved =false;
Connection conn = null;
PreparedStatement pstmt = null;
conn = Tools.getConn();
String sql = "insert into shopcart values(?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, shopCart.getId());
pstmt.setInt(2, shopCart.getOrderId());
pstmt.setInt(3, shopCart.getBook().getId());
pstmt.setDouble(4, shopCart.getPrice());
int temp = pstmt.executeUpdate();
if(temp>0)
{
isSaved = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isSaved;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -