JAVA jdbc获取数据库连接
JDBC获取数据库连接的帮助类
import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; public class jdbcUtil { private static String driver; private static String url; private static String id; private static String password; //加载文件,获取配置参数 static{ Properties prop=new Properties(); InputStream is; try { is = jdbcUtil.class.getClassLoader() .getResourceAsStream("com/sy/db/db.properties"); prop.load(is); driver = prop.getProperty("driver"); url = prop.getProperty("url"); id = prop.getProperty("id"); password = prop.getProperty("password"); } catch (Exception e1) { e1.printStackTrace(); } } //注册驱动 static{ try { Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } //取得连接 public static Connection getMysqlConnection(){ Connection conn=null; try { conn=DriverManager.getConnection(url,id,password); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭流 public static void close(ResultSet rs){ if(null!=rs){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close((Statement state){ if(null!=rs){ try { state.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close(Connection conn){ if(null!=rs){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }