首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

以properties形式来联接数据库

2012-11-21 
以properties方式来联接数据库1:在WEB工程SEC目录下新建一个database.properties文件,里面保存连接数据库

以properties方式来联接数据库

1:在WEB工程SEC目录下新建一个database.properties文件,里面保存连接数据库信息2:public class DBUtil {   private String driver;   private String url;   private String user;   private String psw;   private static DBUtil dbutil=null;   private DBUtil() throws IOException, ClassNotFoundException{   InputStream is=this.getClass().getClassLoader().getResourceAsStream("database.properties");   Properties p=new Properties();   p.load(is);   driver=p.getProperty("driver");   url=p.getProperty("url");   user=p.getProperty("user");   psw=p.getProperty("psw");      Class.forName(driver);   }   /** * 获得数据库帮助类唯一实例 */  synchronized public static DBUtil getInstance() throws IOException, ClassNotFoundException{   if(dbutil==null){   dbutil=new DBUtil();   }   return dbutil;   }  /** * 获取数据库连接 无参数 */   synchronized public Connection getConnection() throws SQLException{  return DriverManager.getConnection(url,user,psw);  }

热点排行