/** * Start Derby Network Server using the property derby.drda.startNetworkServer. This property can * be set as a system property or or by setting in derby.properties file. Setting this property to * true , starts the Network Server when Derby boots up. The port at which the Derby Network * Server listens to can be changed by setting the derby.drda.portNumber property. By default, the * server starts at port 1527 Server output goes to derby.log */ private static void startWithProperty() throws Exception { System.out.println("Starting Network Server"); System.setProperty("derby.drda.startNetworkServer", "true"); // Booting derby Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); }
/** Creates a JDBC data source with the given specification. */ public static DataSource dataSource( String url, String driverClassName, String username, String password) { if (url.startsWith("jdbc:hsqldb:")) { // Prevent hsqldb from screwing up java.util.logging. System.setProperty("hsqldb.reconfig_logging", "false"); } BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); return dataSource; }
/** 利用属性文件中的信息连接数据库 * */ public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); String fileName = "QueryDB.properties"; FileInputStream in = new FileInputStream(fileName); props.load(in); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); }
// @Test TODO. commented out. Can't get test to work without an application server. public void testCreateSql2oFromJndi() throws Exception { System.setProperty( Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming"); InitialContext ic = new InitialContext(); ic.createSubcontext("java:"); ic.createSubcontext("java:comp"); ic.createSubcontext("java:comp/env"); JDBCDataSource datasource = new JDBCDataSource(); datasource.setUrl(url); datasource.setUser(user); datasource.setPassword(pass); ic.bind("java:comp/env/Sql2o", datasource); System.out.println("Datasource initialized."); Sql2o jndiSql2o = new Sql2o("Sql2o"); assertTrue(jndiSql2o != null); }
/** * Gets a connection from the properties specified in the file database.properties * * @return the database connection */ public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); try (InputStream in = Files.newInputStream(Paths.get("database.properties"))) { props.load(in); } String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); }
/** * 初始化连接池 * * @param props * @param show_sql */ private static final void initDataSource(Properties dbProperties) { try { if (dbProperties == null) { dbProperties = new Properties(); dbProperties.load(DBManager.class.getResourceAsStream(CONFIG_PATH)); } // Class.forName(dbProperties.getProperty("jdbc.driverClass")); for (Object key : dbProperties.keySet()) { String skey = (String) key; if (skey.startsWith("jdbc.")) { String name = skey.substring(5); cp_props.put(name, dbProperties.getProperty(skey)); if ("show_sql".equalsIgnoreCase(name)) { show_sql = "true".equalsIgnoreCase(dbProperties.getProperty(skey)); } } } dataSource = (DataSource) Class.forName(cp_props.getProperty("datasource")).newInstance(); if (dataSource.getClass().getName().indexOf("c3p0") > 0) { // Disable JMX in C3P0 System.setProperty( "com.mchange.v2.c3p0.management.ManagementCoordinator", "com.mchange.v2.c3p0.management.NullManagementCoordinator"); } log.info("Using DataSource : " + dataSource.getClass().getName()); BeanUtils.populate(dataSource, cp_props); Connection conn = getConnection(); DatabaseMetaData mdm = conn.getMetaData(); log.info( "Connected to " + mdm.getDatabaseProductName() + " " + mdm.getDatabaseProductVersion()); closeConnection(); } catch (Exception e) { e.printStackTrace(); throw new DBException(e); } }
/** * @param contexto * @param pathReporte */ private void compila(ExternalContext contexto, String pathReporte) { System.setProperty("jasper.reports.compile.temp", contexto.getRealPath(pathReporte)); }