Ejemplo n.º 1
0
 @Bean
 public DataSource dataSource() throws SQLException {
   SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
   dataSource.setDriver(new Driver());
   dataSource.setUrl("jdbc:mysql://localhost:3306/pensiondb");
   dataSource.setUsername("root");
   dataSource.setPassword("teresa");
   return dataSource;
 }
Ejemplo n.º 2
0
 @Bean
 public DataSource dataSource() {
   SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
   dataSource.setDriverClass(org.postgresql.Driver.class);
   dataSource.setUrl("jdbc:postgresql://localhost/Cinema");
   dataSource.setUsername("postgres");
   dataSource.setPassword("postgres");
   return dataSource;
 }
Ejemplo n.º 3
0
 private DataSource createSimpleDataSource() throws ClassNotFoundException {
   PropsUtil props = new PropsUtil("db.config.properties");
   SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
   dataSource.setDriverClass((Class<? extends Driver>) Class.forName(props.getString("driver")));
   dataSource.setUrl(props.getString("url"));
   dataSource.setUsername("");
   dataSource.setPassword("");
   return dataSource;
 }
Ejemplo n.º 4
0
 /** 构造数据源,仅构造一次. 连接参数从配置文件中读取,可指向本地的开发环境,也可以指向远程的测试服务器。 */
 protected static void buildDataSourceOnce() throws ClassNotFoundException {
   if (dataSource == null) {
     dataSource = new SimpleDriverDataSource();
     dataSource.setDriverClass(
         (Class<? extends Driver>) Class.forName(propertiesLoader.getProperty("jdbc.driver")));
     dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
     dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
     dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
   }
 }
 @SuppressWarnings("unchecked")
 @Bean
 @Autowired
 DataSource getDataSource(Environment env)
     throws ClassNotFoundException, InstantiationException, IllegalAccessException {
   SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
   dataSource.setDriverClass(
       (Class<? extends java.sql.Driver>) Class.forName(env.getProperty("db.driver")));
   dataSource.setUrl(env.getProperty("db.url"));
   dataSource.setUsername(env.getProperty("db.username"));
   dataSource.setPassword(env.getProperty("db.password"));
   return dataSource;
 }
Ejemplo n.º 6
0
  @Bean(name = "dataSource")
  public DataSource dataSource() {
    /*BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/diagram");
    dataSource.setUsername("user");
    dataSource.setPassword("user");
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(true);

    return dataSource;*/

    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
    dataSource.setDriverClass(org.h2.Driver.class);
    dataSource.setUsername("sa");
    dataSource.setUrl(
        "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS diagram");
    dataSource.setPassword("");
    return dataSource;
  }
Ejemplo n.º 7
0
  private static void persistProductList(final List<Product> products) {
    try {
      SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
      dataSource.setDriver(new com.mysql.jdbc.Driver());
      dataSource.setUrl("jdbc:mysql://localhost/inventory");
      dataSource.setUsername("root");
      // dataSource.setPassword("P@ssw0rd");
      JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
      String sql =
          "insert into products (productName, productCode, productType, sellingRate, costRate, createdDate, updatedDate) values ( ?, ?, ?, ?, ?, ?, ?)";

      jdbcTemplate.batchUpdate(
          sql,
          new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
              Product product = products.get(i);
              preparedStatement.setString(1, product.getProductName());
              preparedStatement.setString(2, product.getProductCode());
              preparedStatement.setString(3, product.getProductType());
              preparedStatement.setDouble(
                  4, (product.getSellingRate() == null ? 0 : product.getSellingRate()));
              preparedStatement.setDouble(
                  5, (product.getCostRate() == null ? 0 : product.getCostRate()));
              preparedStatement.setDate(6, new Date((new java.util.Date()).getTime()));
              preparedStatement.setDate(7, new Date((new java.util.Date()).getTime()));
            }

            @Override
            public int getBatchSize() {
              return products.size();
            }
          });

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }