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 妈妈生日祝福语简短励志