封装数据库连接增删改和查询
创建一个JDBCUtil类,记得导入jar包
public class JDBCUtil {
private final static String URL
= "jdbc:mysql://localhost:3306/bj";
private final static String USER
= "root";
private final static String PWD
= "123456";
static {
try {
Class
.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
}
}
public static Connection
geConnection() {
Connection connection
= null
;
try {
connection
= DriverManager
.getConnection(URL
, USER
, PWD
);
} catch (SQLException e
) {
e
.printStackTrace();
}
return connection
;
}
public static int myUpdate(String sql
, Object
[] params
) {
Connection connection
= null
;
PreparedStatement pStatement
= null
;
connection
= geConnection();
int result
= -1;
try {
pStatement
= connection
.prepareStatement(sql
);
if (params
!= null
&& params
.length
>0) {
for (int i
= 0; i
< params
.length
; i
++) {
pStatement
.setObject(i
+ 1, params
[i
]);
}
}
result
= pStatement
.executeUpdate();
return result
;
} catch (SQLException e
) {
e
.printStackTrace();
}
return result
;
}
public static ResultSet
myQuery(String sql
, Object
[] params
) {
Connection connection
= null
;
PreparedStatement pStatement
= null
;
ResultSet rSet
=null
;
connection
= geConnection();
try {
pStatement
= connection
.prepareStatement(sql
);
if (params
!= null
&& params
.length
>0) {
for (int i
= 0; i
< params
.length
; i
++) {
pStatement
.setObject(i
+ 1, params
[i
]);
}
}
rSet
= pStatement
.executeQuery();
return rSet
;
} catch (SQLException e
) {
e
.printStackTrace();
}
return rSet
;
}
public static void closeUtil(PreparedStatement pStatement
,ResultSet resultSet
) {
try {
if(pStatement
!=null
){
pStatement
.close();
}
if(resultSet
!= null
){
pStatement
.close();
}
if(geConnection()!=null
){
geConnection().close();
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
创建一个数据库user
创建一个Main测试增删改类
public class Main {
public static void main(String
[] args
) {
String sql
="update user set user_pwd=? where user_name=?";
Object
[] params
={"123456nice","张三"};
int result
= JDBCUtil
.myUpdate(sql
, params
);
if(result
>0){
System
.out
.println("成功 影响行数为: "+result
);
}
}
}
修改数据的结果:成功 影响行数为: 2
创建一个Main测试查询类
public class Main {
public static void main(String
[] args
) {
String sql2
= "select * from user ";
ResultSet resultSet
= JDBCUtil
.myQuery(sql2
, null
);
List
<Map
<String, Object>> list
= new ArrayList<Map
<String, Object>>();
try {
while (resultSet
.next()) {
Map
<String, Object> map
= new HashMap<String, Object>();
map
.put("id", resultSet
.getInt("id"));
map
.put("user_name", resultSet
.getString("user_name"));
map
.put("user_pwd", resultSet
.getString("user_pwd"));
list
.add(map
);
}
for (Map
<String, Object> map
: list
) {
System
.out
.println(map
);
}
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
查询结果,还可以通过其他方法取值
{user_name=猎人, id=1, user_pwd=123456} {user_name=张三, id=2, user_pwd=123456nice} {user_name=张酸酸, id=3, user_pwd=789456} {user_name=张刘2, id=4, user_pwd=1234789} {user_name=张三, id=11, user_pwd=123456nice} {user_name=张三, id=1123, user_pwd=123456nice}