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[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/*from w ww . java 2 s .co m*/ String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(); // Use TRUNCATE String sql = "TRUNCATE my_table"; // Execute deletion stmt.executeUpdate(sql); // Use DELETE sql = "DELETE FROM my_table"; // Execute deletion stmt.executeUpdate(sql); }
From source file:EmployeeShow.java
public static void main(String[] args) throws Exception { ImageIcon image;//from ww w . java2 s .com Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/c:\\employee"); Statement s = con.createStatement(); ResultSet rs = s.executeQuery("select photo from employee where name = 'Duke'"); if (rs.next()) { Blob photo = rs.getBlob(1); ObjectInputStream ois = null; ois = new ObjectInputStream(photo.getBinaryStream()); image = (ImageIcon) ois.readObject(); } s.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Statement statement = null;//w w w . j a va2 s . co m String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String username = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, username, password); statement = connection.createStatement(); System.out.println("Driver : " + driver); // Put each method call in a separate try block to execute them all System.out.print("\nMaximum rows :"); int maxRows = statement.getMaxRows(); System.out.print(maxRows == 0 ? " No limit" : " " + maxRows); System.out.print("\nMax field size :"); int maxFieldSize = statement.getMaxFieldSize(); System.out.print(maxFieldSize == 0 ? " No limit" : " " + maxFieldSize); System.out.print("\nTimeout :"); int queryTimeout = statement.getQueryTimeout(); System.out.print(queryTimeout == 0 ? " No limit" : " " + queryTimeout); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from w ww .ja va2 s . c om*/ Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate DATE );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.close(); st.close(); conn.close(); } catch (SQLException e) { while (e != null) { String errorMessage = e.getMessage(); System.err.println("sql error message:" + errorMessage); // This vendor-independent string contains a code. String sqlState = e.getSQLState(); System.err.println("sql state:" + sqlState); int errorCode = e.getErrorCode(); System.err.println("error code:" + errorCode); // String driverName = conn.getMetaData().getDriverName(); // System.err.println("driver name:"+driverName); // processDetailError(drivername, errorCode); e = e.getNextException(); } } }
From source file:PrintIndexedResultSet.java
public static void main(String args[]) throws Exception { String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Members"); Statement stmt = con.createStatement(); stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)"); java.util.Date startTime = new java.util.Date(); ResultSet rs = stmt.executeQuery(query); ResultSetMetaData md = rs.getMetaData(); int nColumns = md.getColumnCount(); for (int i = 1; i <= nColumns; i++) { System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t")); }/*from w w w .j ava 2 s. co m*/ while (rs.next()) { for (int i = 1; i <= nColumns; i++) { System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t")); } } java.util.Date endTime = new java.util.Date(); long elapsedTime = endTime.getTime() - startTime.getTime(); System.out.println("Elapsed time: " + elapsedTime); stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*from w w w . j a v a 2s . c om*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); String query = "SELECT stock_id, name, price FROM stocks"; ResultSet resultSet = statement.executeQuery(query); ResultSetMetaData metadata = resultSet.getMetaData(); int precision = metadata.getPrecision(3); int scale = metadata.getScale(3); System.out.println("Precision: " + precision); System.out.println("Scale : " + scale); }
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);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String query = "select * from survey where id > ? and name = ?"; PreparedStatement pstmt = conn.prepareStatement(query); ParameterMetaData paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {//from www. j av 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); String paramTypeName = paramMetaData.getParameterTypeName(param); System.out.println("param SQL type name=" + paramTypeName); } } pstmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; Class.forName(driver);/*www . j a va 2 s . c o m*/ Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); int val = st.executeUpdate("INSERT employee VALUES(" + 13 + "," + "'Aman'" + ")"); System.out.println("1 row affected"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);//from w w w . j a v a 2s . c o m Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a " + "LEFT JOIN countries b ON a.country_id = b.id"; ResultSet resultSet = statement.executeQuery(query); ResultSetMetaData metadata = resultSet.getMetaData(); String tableName = metadata.getTableName(1); System.out.println("Table name of column 'id' = " + tableName); tableName = metadata.getTableName(4); System.out.println("Table name of column 'country name' = " + tableName); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/* w w w . j a va 2s.c om*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); int nullability = metadata.isNullable(1); if (nullability == ResultSetMetaData.columnNullable) { System.out.println("Columns ID can have a null value"); } else if (nullability == ResultSetMetaData.columnNoNulls) { System.out.println("Columns ID does not allowed to have a null value"); } else if (nullability == ResultSetMetaData.columnNullableUnknown) { System.out.println("Nullability unknown"); } }