Example usage for java.sql Connection createStatement

List of usage examples for java.sql Connection createStatement

Introduction

In this page you can find the example usage for java.sql Connection createStatement.

Prototype

Statement createStatement() throws SQLException;

Source Link

Document

Creates a Statement object for sending SQL statements to the database.

Usage

From source file:TestDB.java

 /**
 * Runs a test by creating a table, adding a value, showing the table contents, and removing the
 * table./*  w  w  w. j  a  v a2 s .c om*/
 */
public static void runTest() throws SQLException, IOException
{
   Connection conn = getConnection();
   try
   {
      Statement stat = conn.createStatement();

      stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
      stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

      ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
      if (result.next())
         System.out.println(result.getString(1));
      result.close();
      stat.executeUpdate("DROP TABLE Greetings");
   }
   finally
   {
      conn.close();
   }
}

From source file:com.invariantproperties.udt.AbstractDatabaseTest.java

/**
 * Unload jar file containing our user-defined types.
 * /*ww  w  .ja va  2 s  .  co m*/
 * @throws SQLException
 */
@AfterClass
public static void unloadJarFile() throws SQLException {
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("select sqlj.remove_jar('ip_udt', true)");
    stmt.execute("drop schema invariantproperties");
    stmt.close();
    conn.close();
}

From source file:com.invariantproperties.udt.AbstractDatabaseTest.java

/**
 * Load jar file containing our user-defined types.
 * /*from ww w .j  av a 2 s.  com*/
 * @throws SQLException
 */
@BeforeClass
public static void loadJarFile() throws SQLException {
    File file = new File("userdefinedtypes.jar");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("create schema invariantproperties");
    stmt.execute(String.format("select sqlj.install_jar('file://%s', 'ip_udt', true)", file.getAbsolutePath()));
    stmt.execute("select sqlj.set_classpath('invariantproperties', 'ip_udt')");
    stmt.close();
    conn.close();
}

From source file:com.thoughtworks.go.server.datamigration.M001.java

static boolean required(Connection cxn) throws SQLException {
    try (Statement s = cxn.createStatement()) {
        final ResultSet rs = s
                .executeQuery("SELECT COUNT(*) as remaining FROM pipelineselections WHERE version = 0");
        rs.next();// w w w.  ja v  a  2 s  .  c  om

        return rs.getInt("remaining") > 0;
    }
}

From source file:common.DBTestUtil.java

public static void initDB() throws IOException, SQLException {
    String query = "";
    FileInputStream inputStream = new FileInputStream(TEST_DATA_FILE);

    try {/*from w ww .  j ava  2 s.  c om*/
        query = IOUtils.toString(inputStream);
    } finally {
        inputStream.close();
    }

    Connection connection = DB.getConnection();

    try {
        Statement statement = connection.createStatement();
        statement.execute(query);
    } finally {
        connection.close();
    }
}

From source file:Main.java

static void createTable(Connection conn) throws SQLException {
    Statement stat = null;// w ww  . jav a 2 s  . co m
    stat = conn.createStatement();
    stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    close(stat);
}

From source file:com.earldouglas.xjdl.io.JdbcLicenseLoaderTest.java

@BeforeClass
public static void setupDatabase() throws Exception {
    basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(jdbcDriver.class.getName());
    basicDataSource.setUrl("jdbc:hsqldb:mem:db");
    basicDataSource.setUsername("sa");
    basicDataSource.setPassword("");

    Connection connection = basicDataSource.getConnection();
    Statement statement = connection.createStatement();
    statement.execute("create table licenses (license varchar(512))");
}

From source file:Main.java

/**
 * Helper method to create a Redshift table
 * //from   www .  j a va  2 s  .  co m
 * @param redshiftURL
 *            The JDBC URL of the Redshift database
 * @param loginProperties
 *            A properties file containing the authentication credentials for the database
 * @param tableName
 *            The table to create
 * @param fields
 *            A list of column specifications that will be comma separated in the create table
 *            statement
 * @throws SQLException
 *             Table creation failed
 */
public static void createRedshiftTable(String redshiftURL, Properties loginProperties, String tableName,
        List<String> fields) throws SQLException {
    Connection conn = DriverManager.getConnection(redshiftURL, loginProperties);
    Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE " + tableName + " " + toSQLFields(fields) + ";");
    stmt.close();
    conn.close();
}

From source file:com.us.test.H2Helper.java

/**
 * H2?/*  w w w.j a va  2s  . c om*/
 * @param url ?Uri?
 * @throws SQLException
 */
public static void createOrUpdate(String url, String uname, String upasswd) throws SQLException {
    JdbcConnectionPool cp = JdbcConnectionPool.create(url, uname, upasswd);
    Connection conn = cp.getConnection();
    boolean hasIpTabel = true;
    try {
        hasIpTabel = conn.createStatement().execute("select count(start) from ips");
    } catch (JdbcSQLException e) {
        hasIpTabel = false;
    }
    if (!hasIpTabel) {
        String sql = "create table ips(start BIGINT,end BIGINT,startip varchar(32),endip varchar(32),"
                + " country varchar(255),reurl varchar(255));";
        conn.createStatement().execute(sql);
    }
    conn.close();
    cp.dispose();
}

From source file:Student.java

  public static void checkData() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
  Statement st = conn.createStatement();

  ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
  while (mrs.next()) {
    String tableName = mrs.getString(3);
    System.out.println("\n\n\n\nTable Name: "+ tableName);

    ResultSet rs = st.executeQuery("select * from " + tableName);
    ResultSetMetaData metadata = rs.getMetaData();
    while (rs.next()) {
      System.out.println(" Row:");
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        System.out.println("    Column Name: "+ metadata.getColumnLabel(i + 1)+ ",  ");
        System.out.println("    Column Type: "+ metadata.getColumnTypeName(i + 1)+ ":  ");
        Object value = rs.getObject(i + 1);
        System.out.println("    Column Value: "+value+"\n");
      }//www. j a v a 2 s .  com
    }
  }
}