zoukankan      html  css  js  c++  java
  • JDBC——连接数据库的代码

    第一步:在SCR下创建一个file,写好数据库的相关信息。

    #oracle数据库
    driver=oracle.jdbc.driver.OracleDriver
    jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
    user=scott
    password=yue170305
    
    
    # mysql数据库
    driver=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://localhost:3306/test
    user=root
    password=yue170305
    

     第二步:连接数据库

    public static Connection getConnection() throws Exception {
    		String driverClass = null;
    		String jdbcUrl = null;
    		String user = null;
    		String password = null;
    		// 读取文件
    
    		InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
    				"jdbc.properties");
    		Properties properties = new Properties();
    		properties.load(in);
    		driverClass = properties.getProperty("driver");
    		jdbcUrl = properties.getProperty("jdbcUrl");
    		user = properties.getProperty("user");
    		password = properties.getProperty("password");
    		
    		Driver driver = (Driver) Class.forName(driverClass).newInstance();
    		Properties info = new Properties();
    
    		info.put("user", user);
    		info.put("password", password);
    		Connection connection = driver.connect(jdbcUrl, info);
    		return connection;
    
    	}
    
  • 相关阅读:
    CSU 1122
    CSU 1256
    CSU 1240
    HDU 1874
    CSU 1004
    Problem F CodeForces 16E
    Problem E CodeForces 237C
    Problem C FZU 1901
    12-30
    2016-12-29
  • 原文地址:https://www.cnblogs.com/yue-170305/p/10749754.html
Copyright © 2011-2022 走看看