if(con!=null) con.close();

本文详细介绍了操作数据库的三个核心步骤:打开连接、执行SQL、关闭连接。强调了连接管理的重要性,确保数据库资源的有效利用。

操作数据库分为三步
1:打开连接
2:执行SQL
3:关闭连接
当成功连接数据库,con就不为空,使用完成就关掉它。
建立连接失败,con为空,就不需要关掉

package db; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DBCon { Connection con = null; Statement st = null; ResultSet rs = null; String driver=null; String url = null; String username = null; String password = null; public Connection dbCon() { try { InputStream is=DBCon.class.getClassLoader().getResourceAsStream("db.properties"); Properties prop=new Properties(); try { prop.load(is); driver=prop.getProperty("driver"); url=prop.getProperty("url"); username=prop.getProperty("username"); password=prop.getProperty("password"); } catch (IOException e1) { e1.printStackTrace(); } Class.forName(driver); Class.forName("com.mysql.jdbc.Driver"); url = "jdbc:mysql:///sams?useUnicode=true&characterEncoding=utf8"; username = "root"; password = ""; try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return con; } /* * 增删改 */ public int query(String sql) { int rs = 0; con = dbCon(); try { st = con.createStatement(); rs = st.executeUpdate(sql); } catch (SQLException e) { close(); e.printStackTrace(); } return rs; } /* * 查 */ public ResultSet find(String sql) { try { con = dbCon(); st = con.createStatement(); rs = st.executeQuery(sql); } catch (SQLException e) { close(); e.printStackTrace(); } return rs; } /* * 关闭数据库 */ public void close() { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }有错误
06-13
package com.qiangfeng; import java.sql.*; public class StudentCRDU { public static void main(String[] args) { System.out.println("**************************"); System.out.println("查询结果输出如下:"); testQueryAll(); addStudent(); System.out.println("**************************"); System.out.println("添加数据后---查询结果输出如下:"); testQueryAll(); updateStudent(); System.out.println("**************************"); System.out.println("修改数据后---查询结果输出如下:"); testQueryAll(); deleteStudent(); System.out.println("**************************"); System.out.println("删除数据后---查询结果输出如下:"); testQueryAll(); } public static void testQueryAll() { Connection con = null; Statement st = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123456"); st = con.createStatement(); String sql = "select * from t_student"; rs = st.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); Date birthday = rs.getDate("birthday"); String gender = rs.getString("gender"); System.out.println(id + "\t" + name + "\t" + birthday + "\t" + gender); } } catch (ClassNotFoundException e) { System.err.println("驱动类加载失败!"); e.printStackTrace(); } catch (SQLException e) { System.err.println("查询数据异常!"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void addStudent() { Connection con = null; Statement st = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123456"); st = con.createStatement(); String sql = "INSERT INTO t_student(id,name,birthday,gender) VALUES (5,'张三','2006-07-07','男')"; int affectedRows = st.executeUpdate(sql); System.out.println("新增数据成功,受影响行数:" + affectedRows); } catch (ClassNotFoundException e) { System.err.println("驱动类加载失败!"); e.printStackTrace(); } catch (SQLException e) { if (e.getErrorCode() == 1062) { System.err.println("新增失败:ID=5已存在,请勿重复添加!"); } else { System.err.println("添加数据异常!"); e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void updateStudent() { Connection con = null; Statement st = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123456"); st = con.createStatement(); String sql = "UPDATE t_student SET birthday = '2005-09-09' WHERE name = 'JACK'"; int affectedRows = st.executeUpdate(sql); System.out.println("修改数据成功,受影响行数:" + affectedRows); } catch (ClassNotFoundException e) { System.err.println("驱动类加载失败!"); e.printStackTrace(); } catch (SQLException e) { System.err.println("修改数据异常!"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void deleteStudent() { Connection con = null; Statement st = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123456"); st = con.createStatement(); String sql = "DELETE FROM t_student WHERE id = 2"; int affectedRows = st.executeUpdate(sql); System.out.println("删除数据成功,受影响行数:" + affectedRows); } catch (ClassNotFoundException e) { System.err.println("驱动类加载失败!"); e.printStackTrace(); } catch (SQLException e) { System.err.println("删除数据异常!"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }检查错误
12-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值