JAVA DATABASE CONNECTTION
1:对数据库的查询
public List<CartItem> listCartItem(User u) throws ClassNotFoundException,
SQLException {
ClassforName("commysqljdbcDriver");
Connection c = DriverManager
getConnection(
"jdbc:mysql://127001:3306/shopprojectcharacterEncoding=GBK",
"root", "admin");
Statement st = ccreateStatement();
String s = String
format(
"select cart_item,productname from cart join cart_item on cartid = cart_itemcart_id join product on cart_itemproduct_id = productid where cartuser_id = %d",
ugetId());
ResultSet rs = stexecuteQuery(s);
List<CartItem> l = new ArrayList<CartItem>(getCount(u));
while (rsnext()) {
CartItem ci = new CartItem();
cisetId(rsgetInt(1));
cisetCart_id(rsgetInt(2));
cisetProduct_id(rsgetInt(3));
cisetProduct_name(rsgetString(6));
cisetAmount(rsgetInt(4));
cisetPrice(rsgetFloat(5));
ladd(ci);
}
rsclose();
stclose();
cclose();
return l;
}
2:对数据库的 *** 作之增加
public void addCartItem(CartItem ci) throws ClassNotFoundException,
SQLException {
ClassforName("commysqljdbcDriver");
Connection c = DriverManager
getConnection(
"jdbc:mysql://127001:3306/shopprojectcharacterEncoding=GBK",
"root", "admin");
Statement st = ccreateStatement();
String s = String
format(
"insert into cart_item (cart_id,product_id,amount,price) values (%d,%d,%d,%f)",
cigetCart_id(), cigetProduct_id(), cigetAmount(), ci
getPrice());
stexecute(s);
stclose();
cclose();
}
3:对数据库的 *** 作之删除
public void deleteCartItem(int id) throws ClassNotFoundException,
SQLException {
ClassforName("commysqljdbcDriver");
Connection c = DriverManager
getConnection(
"jdbc:mysql://127001:3306/shopprojectcharacterEncoding=GBK",
"root", "admin");
Statement st = ccreateStatement();
String s = Stringformat("delete from cart_item where id = %d", id);
stexecute(s);
stclose();
cclose();
}
够了吧,记得给分哦,写这么多不容易啊
以上就是关于关于数据库访问的程序(不少于50行,一个程序行数不够的话可以写两个)并写出注释。全部的内容,包括:关于数据库访问的程序(不少于50行,一个程序行数不够的话可以写两个)并写出注释。、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)