标签:turn from action void manager rgs return indicator catch
一. DriverManager 接口
DriverManager 数据库连接驱动接口,用于获取数据库连接对象Connection
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.SQLException; 4 5 public class DriverManagerDemo { 6 7 public static void main(String[] args) { 8 // 数据库连接引用变量 9 Connection conn = null; 10 11 // 加载数据库驱动程序 12 try { 13 Class.forName("com.mysql.jdbc.Driver"); 14 } catch (ClassNotFoundException e) { 15 e.printStackTrace(); 16 } 17 18 try { 19 // 获取数据库连接对象 20 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "1234"); 21 } catch (SQLException e) { 22 e.printStackTrace(); 23 } 24 } 25 }
DBUtil工具类
DBUtil用于封装数据库连接和关闭,简化JDBC开发,提高代码复用性。
1 import java.io.FileReader; 2 import java.io.Reader; 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.util.Properties; 7 8 /** 动态获取数据库连接参数 */ 9 public class DBUtil { 10 private static String driver; 11 private static String url; 12 private static String username; 13 private static String password; 14 15 // 静态代码块,初始化成员变量 16 static { 17 Properties prop = new Properties(); 18 Reader in; 19 try { 20 in = new FileReader("src\\dbconfig.properties"); 21 // 从输入流中加载配置文件 22 prop.load(in); 23 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 28 driver = prop.getProperty("driver"); 29 url = prop.getProperty("url"); 30 username = prop.getProperty("username"); 31 password = prop.getProperty("password"); 32 } 33 34 // 获得一个数据库连接 35 public static Connection open() { 36 try { 37 Class.forName(driver); 38 return DriverManager.getConnection(url, username, password); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 43 // 抛出异常,方法返回null 44 return null; 45 } 46 47 // 关闭数据库连接 48 public static void close(Connection conn) { 49 if (conn != null) { 50 try { 51 conn.close(); 52 } catch (SQLException e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 }
二. Connection接口
数据库连接接口,用于生成Statement和PreparedStatement对象,以及执行Transaction
三. Statement接口和PreparedStatement接口
Statement用于执行静态sql语句以及处理返回结果
1 import java.sql.Connection; 2 import java.sql.ResultSet; 3 import java.sql.SQLException; 4 import java.sql.Statement; 5 6 public class StatementDemo { 7 8 public static void main(String[] args) { 9 String sql = "update from CustomerTb1 set name=? where id=?"; 10 Connection conn = DBUtil.open(); // 获取数据库连接 11 12 // 生成Statement对象用于执行sql语句 13 try { 14 Statement stmt = conn.createStatement(); 15 ResultSet rs = stmt.executeQuery(sql); // 执行查询,返回结果集 16 17 while (rs.next()) { 18 int id = rs.getInt(1); 19 String name = rs.getString(2); 20 String email = rs.getString("email"); 21 22 System.out.println(id + ", " + name + ", " + email); 23 } 24 } catch (SQLException e) { 25 e.printStackTrace(); 26 } finally { 27 DBUtil.close(conn); 28 } 29 } 30 }
PreparedStatement是预定义sql语句,用于动态生成sql与执行sql语句
1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.SQLException; 4 5 public class StatementDemo { 6 7 public static void main(String[] args) { 8 String sql = "update CustomerTb1 set name=? where id=?"; 9 Connection conn = DBUtil.open(); // 获取数据库连接 10 11 /** 预定义sql语句,?为占位符,在运行中动态生成sql语句 */ 12 try { 13 PreparedStatement pstmt = conn.prepareStatement(sql); 14 // 对占位符替换赋值 15 pstmt.setString(1, "gepeng"); 16 pstmt.setInt(2, 3); 17 18 pstmt.executeUpdate(); // 执行更新 19 20 } catch (SQLException e) { 21 e.printStackTrace(); 22 } finally { 23 DBUtil.close(conn); 24 } 25 } 26 }
标签:turn from action void manager rgs return indicator catch
原文地址:http://www.cnblogs.com/Znker/p/6033457.html