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 车行销售祝福语简短