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 生日广州男生祝福语简短