1.项目目录树
2.配置文件config.properties
username = sushe
password = sushe
url = jdbc:mysql://172.16.100.10:3306/sushe?useUnicode=true&characterEncoding=GBK
jdbcDriverName = com.mysql.jdbc.Driver
3.JdbcBean对象,用来存储jdbc链接信息
package com.dlab.bean; public class JdbcBean { private String userName; private String password; private String Url; private String jdbcDriverName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } public String getJdbcDriverName() { return jdbcDriverName; } public void setJdbcDriverName(String jdbcDriverName) { this.jdbcDriverName = jdbcDriverName; } }
4.读取配置文件工具JdbcConfig.java
package com.dlab.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.dlab.bean.JdbcBean; public class JdbcConfig { private JdbcBean jdbcBean; public JdbcConfig() throws IOException{ //使用InputStream读取配置文件 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); Properties configReader = new Properties(); //加载配置文件 configReader.load(in); jdbcBean = new JdbcBean(); jdbcBean.setUserName(configReader.getProperty("username")); jdbcBean.setPassword(configReader.getProperty("password")); jdbcBean.setUrl(configReader.getProperty("url")); jdbcBean.setJdbcDriverName(configReader.getProperty("jdbcDriverName")); } //返回JdbcBean对象 public JdbcBean getConfigInfo(){ return jdbcBean; } }
5.JDBC工具DBUtil.java,获取Connection
package com.dlab.util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.dlab.bean.JdbcBean; public class DBUtil { /** * 采用单例模式 */ private static DBUtil instance = new DBUtil(); private Connection conn; private DBUtil (){ JdbcBean jdbcBean; try { jdbcBean = new JdbcConfig().getConfigInfo(); Class.forName(jdbcBean.getJdbcDriverName()); conn = DriverManager.getConnection(jdbcBean.getUrl(), jdbcBean.getUserName(), jdbcBean.getPassword()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static synchronized DBUtil getInstance (){ return instance; } //返回Connection public Connection getConn(){ return conn; } //Connection的静态关闭方法 public static void close(Connection conn){ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //PreparedStatement的静态关闭方法 public static void close(PreparedStatement pstmt){ if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } //ResultSet的静态关闭方法 public static void close(ResultSet rs){ if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
6.测试程序DBTest.java
package com.dlab.dbtest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.dlab.util.DBUtil; public class DBTest { public static void main(String[] args) { String sql = "select * from Admin"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DBUtil.getInstance().getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString("Admin_Username")); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs); DBUtil.close(pstmt); DBUtil.close(conn); } } }