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);/* w w w .jav a 2 s . com*/ 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(); stmt.executeUpdate("DROP TABLE my_table"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); st.executeUpdate("CREATE DATABASE myDB"); }
From source file:Main.java
public static void main(String args[]) throws Exception { String connectionURL = "jdbc:mysql://localhost:3306/test"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(connectionURL, "login", "password"); Statement stmt = con.createStatement(); stmt.execute("CREATE TRIGGER obs_update BEFORE UPDATE ON obs " // + "FOR EACH ROW "// + "BEGIN "// + "IF OLD.voided = 0 AND NEW.voided = 1 THEN "// + " DELETE FROM emp WHERE id = OLD.obs_id; "// + "ELSE "// + " UPDATE emp SET emp.revision_token = NOW() "// + " WHERE NEW.id = emp.id; "// + "END IF; "// + "END;"); con.close();//from w w w .j a v a2s .com }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.executeUpdate();/* w w w .j a v a 2s .co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.executeUpdate(); rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
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 .j av a 2 s . com 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(); ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM my_table"); // Get the number of rows from the result set resultSet.next(); int rowcount = resultSet.getInt(1); }
From source file:ExecuteExample.java
public static void main(String args[]) { try {/*from w ww. ja v a 2 s. c o m*/ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.out.println("JDBC-ODBC driver failed to load."); return; } try { Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE SalesHistory(ProductID NUMBER,Price CURRENCY, TrnsDate DATE)"); stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*from www. j a v a 2s .c o m*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); if (metadata.isAutoIncrement(1)) { System.out.println("Column ID is an auto-increment column"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);/* w ww. j a v a2 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); Statement stmt = connection.createStatement(); stmt.execute("CREATE TYPE object2 AS OBJECT(col_string2 VARCHAR(30), col_integer2 NUMBER)"); stmt.execute("CREATE TYPE object1 AS OBJECT(col_string1 VARCHAR(30), col_integer2 object2)"); stmt.execute("CREATE TABLE object1_table(col_integer NUMBER, col_object1 object1)"); }
From source file:Main.java
public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName(DRIVER);//from ww w.j a v a 2 s .c om Connection con = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD); ResultSet rs = con.createStatement().executeQuery(QUERY); if (rs != null) { System.out.println("Column Type\t\t Column Name"); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(rsmd.getColumnTypeName(i) + "\t\t\t" + rsmd.getColumnName(i)); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost:3306/"; String dbName = "jdbc4"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url + dbName, userName, password); Statement st = conn.createStatement(); int rows = st.executeUpdate("INSERT INTO Copyemployee SELECT * FROM employee"); if (rows == 0) { System.out.println("Don't add any row!"); } else {//from www . ja v a 2 s . co m System.out.println(rows + " row(s)affected."); conn.close(); } }