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 java.sql.Statement; import java.util.Properties; /** * jdbc的工具类
用这个类的时候前提是 首先要有一个db.properties 文件 在src目录下可以直接创建 * @author Administrator * */ public class JDBCUtils { static Properties properties=null; static{ properties=new Properties(); try { properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 返回数据库的连接 */ public static Connection getConnection(){ try { Class.forName(properties.getProperty("mysqlDriver")); return DriverManager.getConnection(properties.getProperty("mysqlUrl"), properties.getProperty("mysqlUser"), properties.getProperty("mysqlPassword")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public static void close(Connection con){ if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection con,PreparedStatement ps,ResultSet rs) { if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection con,PreparedStatement ps){ if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection con,Statement stst){ if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stst!=null){ try { stst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }