静态类:
静态类方法实际上属于类,
在类加载时被加载器载入内存中。
不同任何类实例相关,也不允许访问任何实例属性;
因此这些方法可以被共享,可以通过类名来调用而不用先实例化一个对象
1.静态常量,获取getConnection()参数
private static String url = "jdbc:mysql://localhost/test?
useUnicode=true&characterEncoding=utf-8";
private static String user = "name";
private static String passwd = "passwd";
2.getConn()获取数据库连接,返回Connection对象,供CURD方法直接调用
public static Connection getConn(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, passwd);
System.out.println("链接成功 " + connection.getClass().getName() + "\n"
+ connection.getMetaData().getURL());
return connection;
} catch (ClassNotFoundException|SQLException e) {
e.printStackTrace();
return null;
}
}
3.insertIntoDB(String sql)插入方法(C);
public static String insertIntoDB(String sql) throws SQLException {
Connection connection = DBUtils.getConn();
Statement statement = connection.createStatement();
int resultFlag = statement.executeUpdate(sql);
if (resultFlag == 1) {
DBUtils.close(null, statement, connection);
return "insert data successed!";
} else {
return "insert data failed!";
}
}
4.UpdateDataOfDB(String sql)更新方法(U);
public static String UpdateDataOfDB(String sql) throws SQLException {
Connection connection = DBUtils.getConn();
Statement statement = connection.createStatement();
int resultFlag = statement.executeUpdate(sql);
if (resultFlag == 1) {
DBUtils.close(null, statement, connection);
return "update data successed!";
} else {
return "update data failed!";
}
}
5.selectUser(String sql)查询方法(R);
public static ResultSet SelectUser(String sql) throws SQLException {
Connection connection = DBUtils.getConn();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
return resultSet;
}
6.DeleteDataOfDB(String sql)删除方法(D);
public static String DeleteDataOfDB(String sql) throws SQLException {
Connection connection = DBUtils.getConn();
Statement statement = connection.createStatement();
int resultFlag = statement.executeUpdate(sql);
if (resultFlag == 1) {
DBUtils.close(null, statement, connection);
return "Delete data successed!";
} else {
return "Delete data failed!";
}
}
7.Close()方法关闭连接,关闭顺序与连接建立顺序相反
public static void close(ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
state = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
1.插入测试
String sql = "insert into test values(null, '王老五', 'admin')";
System.out.println(DBUtils.insertIntoDB(sql));
结果:
11 王老五 admin
2.修改测试
String sql = "update test set username = '喵帕斯' where id='11'";
System.out.println(DBUtils.UpdateDataOfDB(sql));
结果:
11 喵帕斯 admin
3.查询测试
String sql = "select * from test where id='10'";
ResultSet resultSet = DBUtils.SelectUser(sql);
while (resultSet.next()) {
String username = resultSet.getString("username");
String password = resultSet.getString("password");
System.out.println("username" + username);
System.out.println("password:" + password);
}
结果:
username喵帕斯
password:admin
4.删除测试
String sql = "delete from test where id = '10'";
System.out.println(DBUtils.DeleteDataOfDB(sql));
结果:
id username password
------ --------- ----------
1 test1 123
2 test 234
4 345 345
5 张三 admin
7 345 345
8 张三 admin
9 张三 admin
11 喵帕斯 admin
id为“10”的用户已删除