1、java數據庫連接、查詢、更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
nameget= '%' +nameget+ '%' ; string sqlgname = "select * from goods where gname like ?" ; try { pstmt = conn.preparestatement(sqlgname); pstmt.setstring( 1 , nameget); rs = pstmt.executequery(); while (rs.next()) { int gid = rs.getint( "gid" ); string gname = rs.getstring( 2 ); double gprice = rs.getdouble( 3 ); int gnum = rs.getint( 4 ); goods goods = new goods(gid,gname,gprice,gnum); goodslist.add(goods); } } catch (sqlexception e) { e.printstacktrace(); } finally { dbclose.queryclose(pstmt, rs, conn); } |
2、連接數據庫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public final class dbconn { public static connection getconn() { connection conn = null ; string user = "root" ; string passwd = "root" ; string url = "jdbc:mysql://localhost:3306/shop" ; //已加載完驅動 try { class .forname( "com.mysql.jdbc.driver" ); conn = drivermanager.getconnection(url,user,passwd); } catch (sqlexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } return conn; } } |
這篇文章就介紹到這,下一篇將為大家更好的更相關的文章。
原文鏈接:https://blog.csdn.net/lxl121181/article/details/79174842