Example usage for java.sql Statement executeUpdate

List of usage examples for java.sql Statement executeUpdate

Introduction

In this page you can find the example usage for java.sql Statement executeUpdate.

Prototype

int executeUpdate(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Usage

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    @Test
public void DataSourceWrapper() throws Exception {
   List<DataSourceWrapper> dataSourceWrappers = new ArrayList<DataSourceWrapper>();
   dataSourceWrappers.add(new DataSourceWrapper("dbKey1","rw", DataSourceFactory.getMySQLDataSource(1), DBType.MYSQL));
   dataSourceWrappers.add(new DataSourceWrapper("dbKey2","r", DataSourceFactory.getMySQLDataSource(2), DBType.MYSQL));

   TGroupDataSource ds = new TGroupDataSource();
   ds.setDbGroupKey("myDbGroupKey");
   ds.init(dataSourceWrappers);/*from w w  w  .  j  a  v  a2s .  c  om*/

   Connection conn = ds.getConnection();

   //Statementcrud
   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
   assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   assertEquals(stmt.executeUpdate("delete from crud"), 1);
   rs.close();
   stmt.close();

   //PreparedStatementcrud
   String sql = "insert into crud(f1,f2) values(?,?)";
   PreparedStatement ps = conn.prepareStatement(sql);
   ps.setInt(1, 10);
   ps.setString(2, "str");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2=?";
   ps = conn.prepareStatement(sql);
   ps.setString(1, "str2");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "select f1,f2 from crud";
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   rs.close();
   ps.close();

   sql = "delete from crud";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   conn.close();
}

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    () throws Exception {
   DataSource ds1 = DataSourceFactory.getMySQLDataSource(1);
   DataSource ds2 = DataSourceFactory.getMySQLDataSource(2);
   DataSource ds3 = DataSourceFactory.getMySQLDataSource(3);
      //from w  w w.  j  a v  a2 s . c o  m
      
   //db3db2db1
   TGroupDataSource ds = new TGroupDataSource();
   DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "r10w", ds1, DBType.MYSQL);
   DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r20", ds2, DBType.MYSQL);
   DataSourceWrapper dsw3 = new DataSourceWrapper("db3", "r30", ds3, DBType.MYSQL);
   ds.init(dsw1, dsw2, dsw3);

   Connection conn = ds.getConnection();

   //Statementcrud
   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
   assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   assertEquals(stmt.executeUpdate("delete from crud"), 1);
   rs.close();
   stmt.close();

   //PreparedStatementcrud
   String sql = "insert into crud(f1,f2) values(10,'str')";
   PreparedStatement ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2='str2'";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "select f1,f2 from crud";
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   rs.close();
   ps.close();

   sql = "delete from crud";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   conn.close();
}

From source file:com.aerothai.database.building.BuildingService.java

/**
* Method to check whether uname and pwd combination are correct
* 
* @param uname//  www. j a  va 2 s  .  com
* @param pwd
* @return
* @throws Exception
*/
public String updateBuilding(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "update");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.building.BuildingService.java

/**
 * Method to check whether uname and pwd combination are correct
 * //from ww  w  .  ja v a 2 s  .  com
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String deleteBuilding(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "delete");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.os.OsService.java

/**
* Method to check whether uname and pwd combination are correct
* 
* @param uname//from w w  w  .ja v  a  2  s  .c  o  m
* @param pwd
* @return
* @throws Exception
*/
public String updateOs(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "update");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.os.OsService.java

/**
 * Method to check whether uname and pwd combination are correct
 * /*  www.jav  a 2 s.c o  m*/
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String deleteOs(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "delete");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.building.BuildingService.java

/**
 * Method to check whether uname and pwd combination are correct
 * //  w  w w.j a  v a2s .  c o m
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String createBuilding(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "create");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.os.OsService.java

/**
 * Method to check whether uname and pwd combination are correct
 * //from  w  w w .  j a v a  2 s  . c  o m
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String createOs(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "create");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:net.ontopia.topicmaps.db2tm.CSVImport.java

public void importCSV(InputStream csvfile) throws Exception {
    // Execute statements
    try {//from   w  w  w  .  ja  v a  2s.  c  o  m
        System.out.println("TABLE: " + table);

        if (cleartable) {
            String delsql = "delete from " + table;
            log.debug("DELETE: {}", delsql);
            Statement delstm = conn.createStatement();
            delstm.executeUpdate(delsql);
            //! conn.commit();
        }

        // get hold of column metadata
        List<String> colnames = new ArrayList<String>();
        List<Integer> coltypes_ = new ArrayList<Integer>();
        ResultSet rs = conn.getMetaData().getColumns(null, null, table, null);
        try {
            while (rs.next()) {
                String colname = rs.getString(4);
                int coltype = rs.getInt(5);
                colnames.add(colname);
                coltypes_.add(new Integer(coltype));
            }
        } finally {
            rs.close();
        }
        int[] coltypes = new int[coltypes_.size()];
        for (int i = 0; i < coltypes.length; i++) {
            coltypes[i] = coltypes_.get(i).intValue();
        }

        String[] qmarks = new String[coltypes.length];
        for (int i = 0; i < qmarks.length; i++) {
            qmarks[i] = "?";
        }

        String sql = "insert into " + table + " (" + StringUtils.join(colnames, ", ") + ") values ("
                + StringUtils.join(qmarks, ", ") + ")";
        log.debug("INSERT: {}", sql);
        PreparedStatement stm = conn.prepareStatement(sql);

        LineNumberReader reader = new LineNumberReader(new InputStreamReader(csvfile));
        CSVReader csvreader = new CSVReader(reader, separator, quoteCharacter);

        // Ignore first X lines
        for (int i = 0; i < ignorelines; i++) {
            String[] tuple = csvreader.readNext();
            if (tuple == null)
                break;
        }

        // HACK: override date+datetime formats
        JDBCUtils.df_date = new SimpleDateFormat("dd.MM.yyyy");
        JDBCUtils.df_datetime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

        // Process input
        log.debug("[{}]", StringUtils.join(colnames, ", "));
        String[] tuple = null;
        try {
            while ((tuple = csvreader.readNext()) != null) {
                for (int i = 0; i < tuple.length; i++) {
                    System.out.println("V:" + (i + 1) + " " + colnames.get(i) + ":" + coltypes[i] + " "
                            + tuple[i].length() + "'" + tuple[i] + "'");
                    JDBCUtils.setObject(stm, i + 1, tuple[i], coltypes[i]);
                }
                stm.execute();
            }
        } catch (Exception e) {
            throw new OntopiaRuntimeException(
                    "Cannot read line " + reader.getLineNumber() + ": " + Arrays.asList(tuple), e);
        }
        conn.commit();

    } catch (Exception e) {
        //conn.rollback();
        throw e;
    }
}

From source file:com.aerothai.database.dept.DeptService.java

/**
* Method to check whether uname and pwd combination are correct
* 
* @param uname/*from w w w  . j  ava  2  s.com*/
* @param pwd
* @return
* @throws Exception
*/
public String updateDept(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "update");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}