首页 > 代码库 > JDBC开发模式
JDBC开发模式
一】代码模块———Demo.java
public class Demo { private static Connection connection; private static Statement statement; private static ResultSet rs; public static void main(String[] args) { connection = JDBCUtils.getConnection(); try { statement = connection.createStatement(); rs = statement.executeQuery(SqlMapping.QEURY_ALL); while (rs.next()) { System.out.println(rs.getInt("id") + " : " + rs.getString("name") + " : " + rs.getString("gender") + " : " + rs.getString("salary")); } } catch (SQLException e) { e.printStackTrace(); } JDBCUtils.closeStream(rs, statement, connection); }
二】工具模块———JdbcUtil.java
public class JDBCUtils { private static Properties properties; //得到数据库驱动 static { InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("com/suse/jdbc/db.properties"); properties = new Properties(); try { properties.load(inputStream); Class.forName(properties.getProperty("driver")); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //得到数据库连接 public static Connection getConnection(){ Connection connection = null; try { connection = DriverManager.getConnection(properties.getProperty("url"), properties); } catch (SQLException e) { e.printStackTrace(); } return connection; } //关闭流 public static void closeStream( ResultSet rs, Statement stmt, Connection conn) { if (null != rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != stmt) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
三】配置文件模块———db.properties
driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/mydb user = root password = root
JDBC开发模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。