本次将使用两种方式实现JDBC,分别是使用工具类(DBUtil类)和不使用工具类。
1、不使用工具类,直接在测试类的main函数中写出连接数据库以及增删改查的过程。代码如下所示:
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
// 常用的三种变量分别属于Connection、PrepareStatement以及ResultSet类。(使用工具类也需要这三种类型)。
try {
Class.forName("com.mysql.jdbc.Driver");
//在类加载器中加载driver
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java7?useSSL=true&characterEncoding=utf-8" +
"&user=root&password=123");
//这里localhost是mysql的端口号,java7是数据库名称,user和password填自己的用户名和密码
System.out.println("数据库连接成功"+conn);
//比较关键的是正确编写sql语句
String sql = "select * from tb_user";
//获取存取sql的对象
pstm = conn.prepareStatement(sql);
//执行sql语句得到结果
rs = pstm.executeQuery();
while(rs.next()){ //循环到最后一条记录为止
System.out.println("用户id:"+rs.getInt(1));
System.out.println("用户名:"+rs.getString(2));
System.out.println("用户密码:"+rs.getString(3));
}
} catch (Exception e) {
}finally{
//这里要注意它们的关闭顺序,先定义的后关闭,后定义的先关闭。
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
;
}
else if (pstm != null) {
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}else if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2、使用工具类DBUtil,工具类只负责与数据库的连接,需要专门写一个表示某个表的类,采用面向对象的方法。
DBUtil:
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/java7? useSSL=true&characterEncoding=utf-8";
private static String user = "root";
private static String password = "123";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//2、创建连接
public static Connection get_Conn() throws Exception {
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println("连接成功"+conn);
return conn;
}
//关闭连接
public static void get_CloseConn(ResultSet rs,PreparedStatement pstm,Connection conn) throws SQLException {
//同样要注意三种变量的关闭顺序
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
}
public static void main(String[] args)
{
try {
get_Conn();
} catch (Exception e) {
e.printStackTrace();
}
}
User类:
3种属性分别对应user表的三列,再加上构造函数、getter、setter方法。
在不使用工具类中已给出了查询的代码,此处补充“增删改”的相关代码。
//增加一条记录
public void addUser(User user) {
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("insert into tb_user(username,password) values(?,?)");
//?是占位符,此处未给出值的变量可在之后的语句中给出
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
int i = pstm.executeUpdate(); //执行更新语句
if (i > 0) {
System.out.println("增加成功");
} else {
System.out.println("增加失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//关闭连接
DBUtil.get_CloseConn(null, pstm, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除一条记录
public void delUser(int userid){
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("delete from tb_user where id = ?");
pstm.setInt(1, userid);
int i = pstm.executeUpdate();
if (i > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
DBUtil.get_CloseConn(null, pstm, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//修改一条记录
public void changePassword(int ID,String password){ //改密码
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("update tb_user set password = ? where id = ?");
pstm.setString(1, password);
pstm.setInt(2, ID);
int i = pstm.executeUpdate();
if (i > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
DBUtil.get_CloseConn(null, pstm, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
以上就是使用JDBC连接的两种方法,对于一个较大的项目来说显然使用DBUtil工具类要更合适,其实无论做什么项目,使用JDBC的代码都是差不多的,只要多加练习多写代码,就可以熟练地运用它了。
注意连接最后一定要释放,要养成释放连接的好习惯,避免内存的过多占用。学会JDBC只是使用数据库的第一步,关于Web开发我们要学习的还有很多,除了听老师讲,自己也要主动查阅相关书籍,多加练习。