JavaScript 关闭页面(IE, firefox, Chrome)兼容问题
从表单上传文件到数据库或服务器

使用properties文件配置数据库

Jimmy posted @ 2012年3月10日 02:16 in JSP+MySQL , 1640 阅读

先把数据库连接参数写到.properties文件中,文件内容如下:

URL=jdbc:mysql://localhost/#这里是数据库名

USER=#这里是连接数据库用户名
PASSWD=#连接数据库的密码
DRIVER=com.mysql.jdbc.Driver
 
把.properties文件放到当前应用的WEB/INF/classes子目录下,如果WEB/INF没有classes目录,就自行建立,再放到此目录下。
 
读取properties文件的程序为:
	        public Properties getPropertiesFile(String addr)
	        {
		        try{
			    Properties properties  = new Properties();
			    FileInputStream file = new FileInputStream(addr);
			    properties.load(file);
			    return properties;
		            }catch(IOException e){
			    e.printStackTrace();
			    return null;
		           }
	        }
		
           Properties properties = getPropertiesFile("/connection.properties");
		String driver = properties.getProperty("DRIVER");
		String url = properties.getProperty("URL");
		String user = properties.getProperty("USER");
		String pwd = properties.getProperty("PASSWD");
		try{
			Class.forName(driver);
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		try{
			this.connection = DriverManager.getConnection(url,user,pwd);
		}catch(SQLException e){
			e.printStackTrace();
		}

 

Avatar_small
gmail email login 说:
2021年3月14日 17:25

Having email is a essential thing when you use internet, but to use email more effectively is not easy, gmail sign in has the tutorial and tips you need.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter