|
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 |
package
javatest;import
java.sql.*; public
class
DBconn{ // 驱动程序名 final
String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名test // MySQL配置时的用户名 final
String user = "root"; // MySQL配置时的密码 final
String password = "root"; public
Connection conn =null; public
Statement statement=null; public
DBconn() { try
{ // 加载驱动程序 Class.forName(driver); // 连续数据库 conn = DriverManager.getConnection(url, user, password); statement = conn.createStatement(); // if(!conn.isClosed()) // System.out.println("Succeeded connecting to the Database!"); } catch(Exception e) { } } public
void
DBcolse() { try { this.conn.close(); } catch(Exception e) { e.printStackTrace(); System.out.println("数据库关闭失败!"); } }} |
|
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 |
DBconn db1=new
DBconn(); ResultSet rs =null; try{ System.out.println("用户请求注册"); out.println("please input name"); user_name =in.readLine().trim(); out.println("please input password"); user_password =in.readLine().trim(); }catch
(Exception e) { System.out.println(e); } try { String sql= "select * from user where name=‘"+user_name+"‘"; rs = db1.statement.executeQuery(sql); if(rs.next()) { System.out.println(user_name+"已经注册"); out.println(user_name+"已经注册"); } else { String sql_insert= "insert into user(name,password) values( ‘"+ user_name+"‘ , ‘"+user_password+"‘ )"; System.out.println( db1.statement.executeUpdate(sql_insert)); System.out.println("注册成功,成功添加了"+user_name+user_password); out.println("注册成功,成功添加了"+user_name+user_password); } } catch
(Exception e) { System.out.println(e); } |
更新数据
String sql= "update unit set "+field_id+" =‘"+value+"‘ where
device_id="+device_id+" and
index_in_device="+unit_id+"";
out.println(sql);
DBconn db1=new
DBconn();
int
rs;
rs =
db1.statement.executeUpdate(sql);
System.out.println("updata number="+rs);
java mysql 简单操作,布布扣,bubuko.com
原文:http://www.cnblogs.com/egai/p/3615251.html