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;
}
}
这篇文章就介绍到这,下一篇将为大家更好的更相关的文章。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短