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 user = "root"; String pass = "root"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); String table = "CREATE TABLE java_DataTypes2(typ_boolean BOOL, " + "typ_byte TINYINT, typ_short SMALLINT, " + "typ_int INTEGER, typ_long BIGINT, " + "typ_float FLOAT, typ_double DOUBLE PRECISION, " + "typ_bigdecimal DECIMAL(13,0), typ_string VARCHAR(254), " + "typ_date DATE, typ_time TIME, " + "typ_timestamp TIMESTAMP, " + "typ_asciistream TEXT, typ_binarystream LONGBLOB, " + "typ_blob BLOB)"; st.executeUpdate(table);//w w w . j a v a2 s. c om con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://192.168.100.100:3306/"; String dbName = "databaseName"; Statement stmt = null;//w ww . ja va 2s .c o m ResultSet result = null; String driver = "com.mysql.jdbc.Driver"; String databaseUserName = "admin"; String databasePassword = "root"; Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword); stmt = conn.createStatement(); result = null; String password, username; result = stmt.executeQuery("select * from userTable where username ='user1' "); if (!result.isBeforeFirst()) { System.out.println("resultset contin no rows"); } while (result.next()) { username = result.getString("username"); password = result.getString("password"); System.out.println(username + " " + password); } conn.close(); }
From source file:JndiDataSource.java
public static void main(String args[]) throws Exception { String sp = "com.sun.jndi.fscontext.RefFSContextFactory"; String file = "file:/e:/JNDI"; String dataSourceName = "jdbc/myDatabase"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, sp); env.put(Context.PROVIDER_URL, file); ctx = new InitialContext(env); bindDataSource(ctx, dataSourceName); DataSource ds = null;//from ww w.j ava 2 s . co m ds = (DataSource) ctx.lookup(dataSourceName); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT Name FROM Employees"); while (rs.next()) System.out.println(rs.getString("name")); ctx.close(); rs.close(); stmt.close(); conn.close(); ctx.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver).newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM emp"); while (res.next()) { int i = res.getInt("ID"); String s = res.getString("name"); System.out.println(i + "\t\t" + s); }/*from www .j a v a 2 s. c o m*/ con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people;"); stat.executeUpdate("create table people (name, occupation);"); PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);"); prep.setString(1, "G"); prep.setString(2, "politics"); prep.addBatch();// ww w . java 2s . co m prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch(); prep.setString(1, "W"); prep.setString(2, "Tester"); prep.addBatch(); conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); ResultSet rs = stat.executeQuery("select * from people;"); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("job = " + rs.getString("occupation")); } rs.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);//w w w .ja va 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(); // Column Name SQLServer Type Java Type String sql = "CREATE TABLE sqlserver_all_table(" + "col_boolean BIT, " // boolean + "col_byte TINYINT, " // byte + "col_short SMALLINT, " // short + "col_int INTEGER, " // int + "col_float REAL, " // float + "col_double DOUBLE PRECISION, " // double + "col_bigdecimal DECIMAL(13,0), " // BigDecimal; can also be NUMERIC(p,s) + "col_string VARCHAR(254), " // String + "col_date DATETIME, " // Date + "col_time DATETIME, " // Time + "col_timestamp TIMESTAMP, " // Timestamp + "col_characterstream TEXT, " // CharacterStream or AsciiStream (< 2 GBytes) + "col_binarystream IMAGE)"; // BinaryStream (< 2 GBytes) stmt.executeUpdate(sql); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver);//from ww w. ja v a 2 s . c o m Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee6"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); System.out.println("Number of Column : " + col); for (int i = 1; i <= col; i++) { String col_name = md.getColumnName(i); System.out.println(col_name); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from w w w. java 2 s . c o m Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name CHAR(5) );"); stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')"); // since there were no errors, commit conn.commit(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); stmt.close(); conn.close(); }
From source file:TestThinApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("select 'Hello Thin driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();/* ww w . j a v a 2 s . c o m*/ stmt.close(); conn.close(); }
From source file:TestOCIApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // or you can use: // DriverManager.registerDriver( // new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@dssnt01", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("select 'Hello OCI driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();/*from w w w .ja v a2 s . co m*/ stmt.close(); conn.close(); }