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 url = "jdbc:mysql://localhost:3306/"; String dbName = "jdbcMysql"; String userName = "root"; String password = "root"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection(url + dbName, userName, password); Statement st = conn.createStatement(); int rows = st.executeUpdate("INSERT INTO myTable SELECT * FROM jdbc4.Copyemployee"); if (rows == 0) { System.out.println("Don't add any row!"); } else {// w ww. j av a 2 s . c om System.out.println(rows + " row(s)affected."); conn.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);// www . 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); Statement stmt = connection.createStatement(); String function = "CREATE OR REPLACE FUNCTION myfunc RETURN VARCHAR IS BEGIN RETURN 'a returned string'; END;"; stmt.executeUpdate(function); }
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,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);//from w w w .j a v a2s. co m pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); Calendar cal = Calendar.getInstance(); // get the TimeZone for "America/Los_Angeles" TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); cal.setTimeZone(tz); while (rs.next()) { // the JDBC driver will use the time zone information in // Calendar to calculate the date, with the result that // the variable dateCreated contains a java.sql.Date object // that is accurate for "America/Los_Angeles". java.sql.Date dateCreated = rs.getDate(2, cal); System.out.println(dateCreated); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);// w ww .j av a2 s . co m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myURL CHAR);"); st.executeUpdate("insert into survey(id) values(01)"); st.executeUpdate("insert into survey(id) values(02)"); Savepoint mySavepoint = conn.setSavepoint("MYSAVEPOINT"); st.executeUpdate("insert into survey(id) values(03)"); conn.commit(); conn.rollback(mySavepoint); st.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);/* ww w . j a v a 2 s . c om*/ 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(); String function = "CREATE OR REPLACE FUNCTION myfuncinout(x IN OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN x:= x||'outvalue'; RETURN 'a returned string'; END;"; stmt.executeUpdate(function); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);//from ww w . j a va 2 s. com 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(); String function = "CREATE OR REPLACE FUNCTION myfuncin(x VARCHAR) RETURN VARCHAR IS " + "BEGIN RETURN 'a return string'||x; END;"; stmt.executeUpdate(function); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);/*w w w.j a v a 2 s .co 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)"); stmt.execute("INSERT INTO object1_table VALUES(1, object1('str1', object2('obj2str1', 123)))"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "org.gjt.mm.mysql.Driver"; Class.forName(driverName);/* ww w. j av a2s . co m*/ String serverName = "localhost"; String mydatabase = "mydatabase"; String url = "jdbc:mysql://" + serverName + "/" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(); String filename = "c:/outfile.txt"; String tablename = "mysql_2_table"; stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);/*ww w .j av a 2 s . c om*/ 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(); String function = "CREATE OR REPLACE FUNCTION myfuncout(x OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "x:= 'outvalue'; " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);// w w w . j a v a 2 s .co 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(); // IN is the default mode for parameter, so both 'x VARCHAR' and 'x IN VARCHAR' are valid String procedure = "CREATE OR REPLACE PROCEDURE myprocin(x VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES(x); " + "END;"; stmt.executeUpdate(procedure); }