Using prepared statements with JDBCTemplate

前端 未结 5 879
既然无缘
既然无缘 2021-01-31 16:16

I\'m using the JDBC template and want to read from a database using prepared statements. I iterate over many lines in a .csv file, and on every line I execute some SQL select qu

相关标签:
5条回答
  • 2021-01-31 16:50
    class Main {
        public static void main(String args[]) throws Exception {
            ApplicationContext ac = new
              ClassPathXmlApplicationContext("context.xml", Main.class);
            DataSource dataSource = (DataSource) ac.getBean("dataSource");
    // DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
    
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    
            String prasobhName = 
            jdbcTemplate.query(
               "select first_name from customer where last_name like ?",
                new PreparedStatementSetter() {
                  public void setValues(PreparedStatement preparedStatement) throws
                    SQLException {
                      preparedStatement.setString(1, "nair%");
                  }
                }, 
                new ResultSetExtractor<Long>() {
                  public Long extractData(ResultSet resultSet) throws SQLException,
                    DataAccessException {
                      if (resultSet.next()) {
                          return resultSet.getLong(1);
                      }
                      return null;
                  }
                }
            );
            System.out.println(machaceksName);
        }
    }
    
    0 讨论(0)
  • 2021-01-31 17:02

    I've tried a select statement now with a PreparedStatement, but it turned out that it was not faster than the Jdbc template. Maybe, as mezmo suggested, it automatically creates prepared statements.

    Anyway, the reason for my sql SELECTs being so slow was another one. In the WHERE clause I always used the operator LIKE, when all I wanted to do was finding an exact match. As I've found out LIKE searches for a pattern and therefore is pretty slow.

    I'm using the operator = now and it's much faster.

    0 讨论(0)
  • 2021-01-31 17:07

    Try the following:

    PreparedStatementCreator creator = new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement updateSales = con.prepareStatement(
            "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
            updateSales.setInt(1, 75); 
            updateSales.setString(2, "Colombian"); 
            return updateSales;
        }
    };
    
    0 讨论(0)
  • 2021-01-31 17:08

    I'd factor out the prepared statement handling to at least a method. In this case, because there are no results it is fairly simple (and assuming that the connection is an instance variable that doesn't change):

    private PreparedStatement updateSales;
    public void updateSales(int sales, String cof_name) throws SQLException {
        if (updateSales == null) {
            updateSales = con.prepareStatement(
                "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");
        }
        updateSales.setInt(1, sales);
        updateSales.setString(2, cof_name);
        updateSales.executeUpdate();
    }
    

    At that point, it is then just a matter of calling:

    updateSales(75, "Colombian");
    

    Which is pretty simple to integrate with other things, yes? And if you call the method many times, the update will only be constructed once and that will make things much faster. Well, assuming you don't do crazy things like doing each update in its own transaction...

    Note that the types are fixed. This is because for any particular query/update, they should be fixed so as to allow the database to do its job efficiently. If you're just pulling arbitrary strings from a CSV file, pass them in as strings. There's also no locking; far better to keep individual connections to being used from a single thread instead.

    0 讨论(0)
  • 2021-01-31 17:11

    By default, the JDBCTemplate does its own PreparedStatement internally, if you just use the .update(String sql, Object ... args) form. Spring, and your database, will manage the compiled query for you, so you don't have to worry about opening, closing, resource protection, etc. One of the saving graces of Spring. A link to Spring 2.5's documentation on this. Hope it makes things clearer. Also, statement caching can be done at the JDBC level, as in the case of at least some of Oracle's JDBC drivers. That will go into a lot more detail than I can competently.

    0 讨论(0)
提交回复
热议问题