Example usage for java.sql Connection createStatement

List of usage examples for java.sql Connection createStatement

Introduction

In this page you can find the example usage for java.sql Connection createStatement.

Prototype

Statement createStatement() throws SQLException;

Source Link

Document

Creates a Statement object for sending SQL statements to the database.

Usage

From source file:Example.java

public static void main(String args[]) {
    try {//from   w w w . j a  v  a2  s  . co  m
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:inventory", "", "");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM inventory ORDER BY price");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        int rowCount = 1;
        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            rowCount++;
        }
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int sum = 0;//from   ww w .ja  v  a 2 s .  co  m
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable");
    while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;
    }
    System.out.println("Sum of column = " + sum);
}

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();
    ResultSet res = st.executeQuery("SELECT col_name  FROM mytable  ORDER BY col_name ASC");
    while (res.next()) {
        int col = res.getInt(1);
        System.out.println(col);/*from  w  ww.  jav  a 2  s.c o  m*/
    }
}

From source file:ResultSetExample.java

public static void main(String args[]) {
    try {/*from   w  w  w  .j a  v a 2  s. co m*/
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price "
                + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' ");
        while (rs.next()) {
            String supplier = rs.getString("SupplierName");
            String product = rs.getString("ProductName");
            int price = rs.getInt("Price");

            System.out.println(supplier + " sells " + product + " for $" + price);
        }

        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    try {//from  w  ww .  ja  va  2  s. c  o  m
        Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

        Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

        Statement s = conn.createStatement();

        ResultSet rs = s.executeQuery("SELECT * FROM tableName");

        while (rs.next()) {
            int id = rs.getInt("GAMEID");
            Document manual = (Document) rs.getObject("MANUAL");
            XMLSerializer serialize = new XMLSerializer(System.out, null);
            serialize.serialize(manual);
        }

    } catch (Throwable e) {
        System.out.println("exception thrown");
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);//w  ww  .j av a  2s  . c om
    Statement st = conn.createStatement();

    PreparedStatement pstmt = conn.prepareStatement("create table survey (id int, name VARCHAR(30) );");

    pstmt.executeUpdate();

    ResultSet 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  www .j  a  va  2 s  .  c o 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();
    int fetchSize = stmt.getFetchSize();

    // Set the fetch size on the statement
    stmt.setFetchSize(100);

    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Change the fetch size on the result set
    resultSet.setFetchSize(100);

}

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  a  v  a 2s . c o 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();
    ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");

    if (rs.next()) {
        Blob blob = rs.getBlob("col_blob");
        long blobLength = blob.length();

        int pos = 1; // position is 1-based
        int len = 10;
        byte[] bytes = blob.getBytes(pos, len);

        InputStream is = blob.getBinaryStream();
        int b = is.read();
    }
}

From source file:TableMaker.java

public static void main(String[] args) throws Exception {
    Class.forName(jdbcDriver);//  w  w  w  .j av  a  2 s .  c  o  m
    url += dbName;
    Connection con = null;
    Statement stmt = null;
    con = DriverManager.getConnection(url);
    stmt = con.createStatement();
    stmt.execute(SQLCreate);
    con.close();
    if (con != null) {
        con.close();
    }
    if (stmt != null) {
        stmt.close();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);/*from  w w w .j  a va 2 s  .co m*/
    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 = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}