List of usage examples for java.sql Connection createStatement
Statement createStatement() throws SQLException;
Statement
object for sending SQL statements to the database. From source file:Main.java
public static void main(String[] args) throws Exception { try {/*ww w .j a v a2 s. c o m*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; FileInputStream fis = new FileInputStream("sometextfile.txt"); Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement createTable = connection.createStatement(); createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)"); String ins = "INSERT INTO source_code VALUES(?,?)"; PreparedStatement statement = connection.prepareStatement(ins); statement.setString(1, "TryInputStream2"); statement.setAsciiStream(2, fis, fis.available()); int rowsUpdated = statement.executeUpdate(); System.out.println("Rows affected: " + rowsUpdated); Statement getCode = connection.createStatement(); ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code"); BufferedReader reader = null; String input = null; while (theCode.next()) { reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream(2))); while ((input = reader.readLine()) != null) { System.out.println(input); } } connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null;//from w w w .j a v a2 s. co m DatabaseMetaData meta = conn.getMetaData(); // The Oracle database stores its table names as Upper-Case, // if you pass a table name in lowercase characters, it will not work. // MySQL database does not care if table name is uppercase/lowercase. // rs = meta.getPrimaryKeys(null, null, "survey"); java.util.List list = new java.util.ArrayList(); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); System.out.println("getPrimaryKeys(): columnName=" + columnName); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from w ww .ja v a 2s.c om*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; FileInputStream fis = new FileInputStream("sometextfile.txt"); Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement createTable = connection.createStatement(); createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)"); String ins = "INSERT INTO source_code VALUES(?,?)"; PreparedStatement statement = connection.prepareStatement(ins); statement.setString(1, "TryInputStream2"); statement.setAsciiStream(2, fis, fis.available()); int rowsUpdated = statement.executeUpdate(); System.out.println("Rows affected: " + rowsUpdated); Statement getCode = connection.createStatement(); ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code"); BufferedReader reader = null; String input = null; while (theCode.next()) { reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream("source"))); while ((input = reader.readLine()) != null) { System.out.println(input); } } connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); PreparedStatement pstmt = null; ParameterMetaData paramMetaData = null; String query = "select * from survey where id > ? and name = ?"; pstmt = conn.prepareStatement(query); paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {/*from w w w . ja va2 s.com*/ System.out.println("db vendor supports ParameterMetaData"); // find out the number of dynamic parameters int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); } pstmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement();/*from w w w. java2 s.c om*/ String query = "select * from survey where 1 = 0"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(query); webRS.execute(conn); // convert xml to a String object StringWriter sw = new StringWriter(); webRS.writeXml(sw); System.out.println(sw.toString()); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); String sqlQuery = "SELECT * FROM survey"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(sqlQuery);/* w w w . j a v a2 s. c o m*/ webRS.execute(conn); // create RowSetMetaData object RowSetMetaData rsMD = (RowSetMetaData) webRS.getMetaData(); System.out.println("rsMD=" + rsMD); if (rsMD == null) { System.out.println("vendor does not support RowSetMetaData"); } else { int columnCount = rsMD.getColumnCount(); System.out.println("columnCount=" + columnCount); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/* w w w . j a v a2s. co m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); st.addBatch("DELETE FROM survey"); st.addBatch("INSERT INTO survey(id, name) " + "VALUES(444, 'ginger')"); // we intentionally pass a table name (animals_tableZZ) // that does not exist st.addBatch("INSERT INTO survey(id, name) " + "VALUES(555, 'lola')"); st.addBatch("INSERT INTO survey(id, name) " + "VALUES(666, 'freddy')"); // Execute the batch int[] updateCounts = st.executeBatch(); checkUpdateCounts(updateCounts); // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false); // start a transaction Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); conn.rollback(); // the preceding inserts will not commit; st.executeUpdate("INSERT INTO survey(id, name) VALUES('33', 'jeff')"); conn.commit(); // end the transaction st = conn.createStatement();// w w w . j a va 2s.co m ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:MainClass.java
public static void main(String args[]) { Connection conn = null; Statement stmt = null;//from w w w.j av a2s . co m try { conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate(EMPLOYEE_TABLE); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')"); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')"); System.out.println("CreateEmployeeTableOracle: main(): table created."); } catch (ClassNotFoundException e) { System.out.println("error: failed to load Oracle driver."); e.printStackTrace(); } catch (SQLException e) { System.out.println("error: failed to create a connection object."); e.printStackTrace(); } catch (Exception e) { System.out.println("other error:"); e.printStackTrace(); } finally { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String tableName = "survey"; String query = "select * from " + tableName + " where 1=0"; Statement stmt = null;/* ww w. jav a2 s . c o m*/ ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } st.close(); conn.close(); }