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;
    
    	}
    
  • 相关阅读:
    Ztree-
    富文本编辑器Ueditor
    通知 弹框
    ResultEntity
    echart
    定时器,定时发邮件JavaMail
    重定向传值
    图片验证码
    异步json发送put或者delete
    异步时间格式转换插件
  • 原文地址:https://www.cnblogs.com/yue-170305/p/10749754.html
Copyright © 2011-2022 走看看