java数据库连接、查询、更新等
1、java数据库连接、查询、更新
nameGet='%'+nameGet+'%';
StringsqlGname="SELECT*FROMGOODSWHEREGNAMELIKE?";
try
{
pstmt=conn.prepareStatement(sqlGname);
pstmt.setString(1,nameGet);
rs=pstmt.executeQuery();
while(rs.next())
{
intgid=rs.getInt("gid");
Stringgname=rs.getString(2);
doublegprice=rs.getDouble(3);
intgnum=rs.getInt(4);
Goodsgoods=newGoods(gid,gname,gprice,gnum);
goodsList.add(goods);
}
}catch(SQLExceptione)
{
e.printStackTrace();
}finally
{
DbClose.queryClose(pstmt,rs,conn);
}
2、连接数据库
publicfinalclassDbConn
{
publicstaticConnectiongetconn()
{
Connectionconn=null;
Stringuser="root";
Stringpasswd="root";
Stringurl="jdbc:mysql://localhost:3306/shop";
//已加载完驱动
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,user,passwd);
}catch(SQLExceptione)
{
e.printStackTrace();
}
catch(ClassNotFoundExceptione)
{
e.printStackTrace();
}
returnconn;
}
}
这篇文章就介绍到这,下一篇将为大家更好的更相关的文章。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短