-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysqlTest.java
More file actions
60 lines (51 loc) · 2.13 KB
/
MysqlTest.java
File metadata and controls
60 lines (51 loc) · 2.13 KB
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
package com.database;
import com.bean.LocalUser;
import com.util.PropertyUtil;
import lombok.extern.slf4j.Slf4j;
import java.sql.PreparedStatement;
import java.util.Properties;
@Slf4j
public class MysqlTest {
private static String url;
private static String user;
private static String password;
// private static Properties prop = new Properties();
public static void insertToTableUser(LocalUser localUser) throws Exception {
url = PropertyUtil.getProperty("url");
user = PropertyUtil.getProperty("user");
password = PropertyUtil.getProperty("password");
Mysql mysql=new Mysql(url,user,password);
String sql="insert into user values (?,?,?,? ); ";
PreparedStatement preparedStatement= mysql.getPrepareStatement(sql);
preparedStatement.setInt(1,localUser.getId());
preparedStatement.setString(2,localUser.getUserName());
preparedStatement.setString(3,localUser.getPassword());
preparedStatement.setInt(4,localUser.getAge());
preparedStatement.executeUpdate();
mysql.close();
}
/**
* PrepareStatement使用动态表名
* @param tableName
* @param localUser
* @throws Exception
*/
public static void insertToDynamicTable(String tableName,LocalUser localUser) throws Exception {
url = PropertyUtil.getProperty("url");
user = PropertyUtil.getProperty("user");
password = PropertyUtil.getProperty("password");
Mysql mysql=new Mysql(url,user,password);
String sql="insert into "+tableName+" values (?,?,?,? ); ";
PreparedStatement preparedStatement= mysql.getPrepareStatement(sql);
preparedStatement.setInt(1,localUser.getId());
preparedStatement.setString(2,localUser.getUserName());
preparedStatement.setString(3,localUser.getPassword());
preparedStatement.setInt(4,localUser.getAge());
preparedStatement.executeUpdate();
mysql.close();
}
public static void main(String[] args) throws Exception {
LocalUser localUser=new LocalUser(11,"lin","lin",25);
insertToDynamicTable("user",localUser);
}
}