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 = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id DECIMAL, name BINARY );"); String sql = "INSERT INTO survey (id) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBigDecimal(1, new BigDecimal("1.00000")); // insert the data pstmt.executeUpdate();/* w w w. ja v a 2s . com*/ ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getString(1)); } rs.close(); stmt.close(); conn.close(); }
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 ww w . j ava2 s . com pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getDate(2)); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, register int );"); String sql = "INSERT INTO survey (register) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBoolean(1, true);//from w w w.j av a2 s .com pstmt.executeUpdate(); pstmt.setBoolean(1, false); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); }
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));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,null)"); st = conn.createStatement();/* w w w . j a v a2 s . com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt("id"); System.out.println("id=" + id); String name = rs.getString(2); System.out.println("name=" + name); if (rs.wasNull()) { System.out.println("name is null"); } else { System.out.println("name is not null"); } System.out.println("---------------"); } rs.close(); st.close(); conn.close(); }
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));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,null)"); st = conn.createStatement();//from w w w.j a va 2 s . c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt(1); System.out.println("id=" + id); String name = rs.getString(2); System.out.println("name=" + name); if (rs.wasNull()) { System.out.println("name is null"); } else { System.out.println("name is not null"); } System.out.println("---------------"); } rs.close(); st.close(); conn.close(); }
From source file:PrintResultSet.java
public static void main(String args[]) throws Exception { String query = "SELECT Name,Description,Qty,Cost FROM Stock"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { System.out.print(rs.getString("Name") + "\t"); System.out.print(rs.getString("Description") + "\t"); System.out.print(rs.getInt("Qty") + "\t"); System.out.println(rs.getFloat("Cost")); }/* www . j a v a 2 s. c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, id2 tinyint, id3 smallint, id4 bigint, id5 real);"); String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); byte b = 1;/*from w ww . ja v a 2 s . co m*/ short s = 2; pstmt.setByte(1, b); pstmt.setShort(2, s); pstmt.setInt(3, 3); pstmt.setLong(4, 4L); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); }
From source file:MainClass.java
public static void main(String[] arguments) { String data = "jdbc:odbc:YourSettings"; try {//from w ww. ja v a 2s . co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(data, "", ""); Statement st = conn.createStatement(); ResultSet rec = st .executeQuery("SELECT * FROM Coal WHERE (Country='" + arguments[0] + "') ORDER BY Year"); while (rec.next()) { System.out.println(rec.getString(1) + "\t" + rec.getString(2) + "\t\t" + rec.getString(3) + "\t" + rec.getString(4)); } st.close(); } catch (Exception e) { System.out.println("Error: " + e.toString() + e.getMessage()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name CHAR(5) );"); stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')"); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); outputResultSet(rs);/*from w ww . j a v a 2 s. com*/ stmt.executeUpdate("drop table survey"); rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY);"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); File file = new File("yourFileName.txt"); long fileLength = file.length(); Reader fileReader = (Reader) new BufferedReader(new FileReader(file)); pstmt.setCharacterStream(1, fileReader, (int) fileLength); int rowCount = pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getBytes(2)); }/*from w w w .j ava2 s .com*/ rs.close(); stmt.close(); conn.close(); }