本文实例讲述了java基于jdbc实现的增删改查操作。分享给大家供大家参考,具体如下:
增删改操作:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package java_web; import java.sql.drivermanager; import java.sql.sqlexception; import com.mysql.jdbc.connection; import com.mysql.jdbc.statement; /** * jdbc curd * @author administrator * */ public class rbacdemo { public final static string url= "jdbc:mysql://localhost:3306/test" ; public final static string username= "root" ; public final static string password= "" ; public final static string driver= "com.mysql.jdbc.driver" ; /** * 插入 */ public static void insert(){ try { class .forname(driver); connection conn = (connection) drivermanager.getconnection(url,username,password); string sql = "insert into test(name,sex)values('fifi2',1),('fifi3',3)" ; statement state = (statement) conn.createstatement(); int result=state.executeupdate(sql); state.close(); conn.close(); system.out.println(result+ "success" ); } catch (classnotfoundexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (sqlexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void update(){ try { class .forname(driver); connection conn = (connection) drivermanager.getconnection(url,username,password); string sql = "update test set name='fifi3aaa' where name='fifi3'" ; statement state = (statement) conn.createstatement(); int result=state.executeupdate(sql); state.close(); conn.close(); system.out.println(result+ "success" ); } catch (classnotfoundexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (sqlexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void delete(){ try { class .forname(driver); connection conn = (connection) drivermanager.getconnection(url,username,password); string sql = "delete from test where name='fifi3aaa'" ; statement state = (statement) conn.createstatement(); int result=state.executeupdate(sql); state.close(); conn.close(); system.out.println(result+ "success" ); } catch (classnotfoundexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (sqlexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void main(string[] args){ //insert(); //update(); delete(); } } |
查询操作:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package java_web; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import com.mysql.jdbc.connection; import com.mysql.jdbc.statement; public class jdbcquerydemo { public final static string url= "jdbc:mysql://localhost:3306/test" ; public final static string username= "root" ; public final static string password= "" ; public final static string driver= "com.mysql.jdbc.driver" ; public static void query(){ try { class .forname(driver); connection conn = (connection) drivermanager.getconnection(url,username,password); string sql = "select id,name,sex from test where id=3" ; statement state = (statement) conn.createstatement(); resultset rs=state.executequery(sql); while (rs.next()){ //rs.getint("id"); int id=rs.getint( 1 ); string name=rs.getstring( 2 ); int sex=rs.getint( 3 ); //string time=rs.getstring("vtime"); system.out.println(id+ "==" +name+ "==" +sex+ "==" ); } rs.close(); state.close(); conn.close(); } catch (classnotfoundexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (sqlexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void main(string[] args) { // todo 自动生成的方法存根 query(); } } |
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/moliyiran/article/details/52419212