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 { 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 v a 2s .c o m System.out.println("db vendor supports ParameterMetaData"); // find out the number of dynamic parameters int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); System.out.println("-------------------"); for (int param = 1; param <= paramCount; param++) { System.out.println("param number=" + param); int paramMode = paramMetaData.getParameterMode(param); System.out.println("param mode=" + paramMode); if (paramMode == ParameterMetaData.parameterModeOut) { System.out.println("the parameter's mode is OUT."); } else if (paramMode == ParameterMetaData.parameterModeIn) { System.out.println("the parameter's mode is IN."); } else if (paramMode == ParameterMetaData.parameterModeInOut) { System.out.println("the parameter's mode is INOUT."); } else { System.out.println("the mode of a parameter is unknown."); } } } pstmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { // Get warnings on ResultSet object ResultSet rs = null;/*from w ww .ja v a 2 s. c o m*/ Connection conn = null; Statement stmt = null; try { conn = getConnection(); // get a java.sql.Connection object stmt = conn.createStatement(); // create a statement // get a result set String sqlQuery = "select id, name from employees"; rs = stmt.executeQuery(sqlQuery); while (rs.next()) { // use result set // // get warnings on the current row of the ResultSet object SQLWarning warning = rs.getWarnings(); if (warning != null) { // process result set warnings } } } catch (SQLException e) { // ignore the exception } finally { // close JDBC resources: ResultSet, Statement, Connection } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar, age int);"); st.executeUpdate("insert into survey (id,name,age ) values (1,'nameValue', 10)"); FilteredRowSet frs = new FilteredRowSetImpl(); frs.setUsername("sa"); frs.setPassword(""); frs.setUrl("jdbc:hsqldb:data/tutorial"); frs.setCommand("SELECT id, name, age FROM survey"); frs.execute();/*from w w w. jav a 2s .com*/ while (frs.next()) { System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":" + frs.getInt("age")); } AgeFilter filter = new AgeFilter(7, 10, 3); frs.beforeFirst(); frs.setFilter(filter); while (frs.next()) { System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":" + frs.getInt("age")); } frs.beforeFirst(); while (frs.next()) { System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":" + frs.getInt("age")); } frs.close(); st.close(); frs.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 . j av a 2 s . com ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column MetaData "); System.out.println("column number " + i); System.out.println(rsMetaData.getColumnDisplaySize(i)); System.out.println(rsMetaData.getColumnLabel(i)); System.out.println(rsMetaData.getColumnName(i)); System.out.println(rsMetaData.getColumnType(i)); System.out.println(rsMetaData.getColumnTypeName(i)); System.out.println(rsMetaData.getColumnClassName(i)); System.out.println(rsMetaData.getTableName(i)); System.out.println(rsMetaData.getPrecision(i)); System.out.println(rsMetaData.getScale(i)); System.out.println(rsMetaData.isAutoIncrement(i)); System.out.println(rsMetaData.isCurrency(i)); System.out.println(rsMetaData.isWritable(i)); System.out.println(rsMetaData.isDefinitelyWritable(i)); System.out.println(rsMetaData.isNullable(i)); System.out.println(rsMetaData.isReadOnly(i)); System.out.println(rsMetaData.isCaseSensitive(i)); System.out.println(rsMetaData.isSearchable(i)); System.out.println(rsMetaData.isSigned(i)); System.out.println(rsMetaData.getCatalogName(i)); System.out.println(rsMetaData.getSchemaName(i)); } 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')"); ResultSet rs = null;// w ww .j a v a 2 s . c o m PreparedStatement ps = null; String query = "select id, name from survey where id = ?"; ps = conn.prepareStatement(query); // specify values for all input parameters ps.setInt(1, 001); // set the first parameter: id // now, PreparedStatement object is ready to be executed. rs = ps.executeQuery(); // iterate the result set object while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); System.out.println("[id=" + id + "][name=" + name + "]"); } // NOTE: you may use PreparedStatement as many times as you want // here we use it for another set of parameters: ps.setInt(1, 002); // set the first parameter: id // now, PreparedStatement object is ready to be executed. rs = ps.executeQuery(); // iterate the result set object while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); System.out.println("[id=" + id + "][name=" + name + "]"); } rs.close(); ps.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);//from w w w.j a v a 2 s . c o m String serverName = "127.0.0.1"; String portNumber = "1521"; String sid = "mydatabase"; String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); CallableStatement cs = connection.prepareCall("{? = call myfuncinout(?)}"); Statement stmt = connection.createStatement(); // Create procedure myproc with no parameters String procedure = "CREATE OR REPLACE PROCEDURE myproc IS " + "BEGIN " + "INSERT INTO oracle_table VALUES('string 1'); " + "END;"; stmt.executeUpdate(procedure); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection m_Connection = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase", "userid", "password"); Statement m_Statement = m_Connection.createStatement(); String query = "SELECT * FROM MyTable"; ResultSet m_ResultSet = m_Statement.executeQuery(query); while (m_ResultSet.next()) { System.out.println(//from ww w . jav a 2 s . co m m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", " + m_ResultSet.getString(3)); } }
From source file:Employee.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); Map map = conn.getTypeMap();//from www.j a v a 2s .com map.put("EMP_DATA", Class.forName("Employee")); conn.setTypeMap(map); ResultSet rs = stmt.executeQuery("SELECT * from Emp"); Employee employee; while (rs.next()) { int empId = rs.getInt("EmpId"); employee = (Employee) rs.getObject("Emp_Info"); System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN); System.out.print(", Name: " + employee.FirstName + " " + employee.LastName); System.out.println( ", Yearly Salary: $" + employee.Salary + " Monthly Salary: " + employee.calcMonthlySalary()); } 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')"); DatabaseMetaData dbMetaData = conn.getMetaData(); ResultSet rs = dbMetaData.getProcedureColumns(conn.getCatalog(), null, "procedureNamePattern", "columnNamePattern"); while (rs.next()) { // get stored procedure metadata String procedureCatalog = rs.getString(1); String procedureSchema = rs.getString(2); String procedureName = rs.getString(3); String columnName = rs.getString(4); short columnReturn = rs.getShort(5); int columnDataType = rs.getInt(6); String columnReturnTypeName = rs.getString(7); int columnPrecision = rs.getInt(8); int columnByteLength = rs.getInt(9); short columnScale = rs.getShort(10); short columnRadix = rs.getShort(11); short columnNullable = rs.getShort(12); String columnRemarks = rs.getString(13); System.out.println("stored Procedure name=" + procedureName); System.out.println("procedureCatalog=" + procedureCatalog); System.out.println("procedureSchema=" + procedureSchema); System.out.println("procedureName=" + procedureName); System.out.println("columnName=" + columnName); System.out.println("columnReturn=" + columnReturn); System.out.println("columnDataType=" + columnDataType); System.out.println("columnReturnTypeName=" + columnReturnTypeName); System.out.println("columnPrecision=" + columnPrecision); System.out.println("columnByteLength=" + columnByteLength); System.out.println("columnScale=" + columnScale); System.out.println("columnRadix=" + columnRadix); System.out.println("columnNullable=" + columnNullable); System.out.println("columnRemarks=" + columnRemarks); }//from www . j a v a 2 s.c o m st.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')"); ResultSet indexInformation = null; DatabaseMetaData meta = conn.getMetaData(); // The '_' character represents any single character. // The '%' character represents any sequence of zero // or more characters. indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "survey", true, true); while (indexInformation.next()) { String dbCatalog = indexInformation.getString("TABLE_CATALOG"); String dbSchema = indexInformation.getString("TABLE_SCHEMA"); String dbTableName = indexInformation.getString("TABLE_NAME"); boolean dbNoneUnique = indexInformation.getBoolean("NON_UNIQUE"); String dbIndexQualifier = indexInformation.getString("INDEX_QUALIFIER"); String dbIndexName = indexInformation.getString("INDEX_NAME"); short dbType = indexInformation.getShort("TYPE"); short dbOrdinalPosition = indexInformation.getShort("ORDINAL_POSITION"); String dbColumnName = indexInformation.getString("COLUMN_NAME"); String dbAscOrDesc = indexInformation.getString("ASC_OR_DESC"); int dbCardinality = indexInformation.getInt("CARDINALITY"); int dbPages = indexInformation.getInt("PAGES"); String dbFilterCondition = indexInformation.getString("FILTER_CONDITION"); System.out.println("index name=" + dbIndexName); System.out.println("table=" + dbTableName); System.out.println("column=" + dbColumnName); System.out.println("catalog=" + dbCatalog); System.out.println("schema=" + dbSchema); System.out.println("nonUnique=" + dbNoneUnique); System.out.println("indexQualifier=" + dbIndexQualifier); System.out.println("type=" + dbType); System.out.println("ordinalPosition=" + dbOrdinalPosition); System.out.println("ascendingOrDescending=" + dbAscOrDesc); System.out.println("cardinality=" + dbCardinality); System.out.println("pages=" + dbPages); System.out.println("filterCondition=" + dbFilterCondition); }/* w ww . j av a2 s. co m*/ st.close(); conn.close(); }