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);//from w w w. ja v a 2 s.c om 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); try { connection.createStatement().execute("select wrong"); } catch (SQLException e) { while (e != null) { String message = e.getMessage(); String sqlState = e.getSQLState(); int errorCode = e.getErrorCode(); driverName = connection.getMetaData().getDriverName(); if (driverName.equals("Oracle JDBC Driver") && errorCode == 123) { } e = e.getNextException(); } } }
From source file:ResultSetMetaDataExample.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); boolean notDone = true; String sqlStr = null;//from w w w . j av a 2s . c o m BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (notDone) { sqlStr = br.readLine(); if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) { ResultSet rs = stmt.executeQuery(sqlStr); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int x = 1; x <= columnCount; x++) { String columnName = rsmd.getColumnName(x); System.out.print(columnName); } while (rs.next()) { for (int x = 1; x <= columnCount; x++) { if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0) System.out.print("$"); String resultStr = rs.getString(x); System.out.print(resultStr + "\t"); } } } else if (sqlStr.startsWith("exit")) notDone = false; } stmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null;//from w ww . j ava2 s . c o m DatabaseMetaData meta = conn.getMetaData(); System.out.println(meta.getDriverVersion()); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); DatabaseMetaData meta = conn.getMetaData(); String sqlKeywords = meta.getSQLKeywords(); System.out.println(sqlKeywords); st.close();/*from w w w . ja va 2s . c o m*/ conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null;/*from w w w. j ava2s. c o m*/ DatabaseMetaData meta = conn.getMetaData(); System.out.println(meta.getDriverName()); st.close(); conn.close(); }
From source file:History.PieChart_DB.java
public static void main(String[] args) throws Exception { String mobilebrands[] = { "IPhone 5s", "SamSung Grand", "MotoG", "Nokia Lumia" }; int statOfRepair = 0; java.util.Date now = new java.util.Date(); String adminDate = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate(); /* Create MySQL Database Connection */ Class.forName("com.mysql.jdbc.Driver"); Connection connect = ConnectDatabase.connectDb("win", "win016"); Statement statement = connect.createStatement(); ResultSet resultSet = statement .executeQuery("SELECT COUNT(transID) AS statRepair FROM `Transaction` WHERE dateTime LIKE \'" + adminDate + "%\' AND action LIKE 'Repair'"); DefaultPieDataset dataset = new DefaultPieDataset(); while (resultSet.next()) { dataset.setValue(resultSet.getString("statRepair"), Double.parseDouble(resultSet.getString("unit_sale"))); }//from w w w . j a v a 2 s . c om JFreeChart chart = ChartFactory.createPieChart("History", // chart title dataset, // data true, // include legend true, false); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File pieChart = new File("Pie_Chart.jpeg"); ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w.j a va 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(); String sql = "CREATE TABLE mysql_all_table(" + "col_boolean BOOL, " // boolean + "col_byte TINYINT, " // byte + "col_short SMALLINT, " // short + "col_int INTEGER, " // int + "col_long BIGINT, " // long + "col_float FLOAT, " // float + "col_double DOUBLE PRECISION, " // double + "col_bigdecimal DECIMAL(13,0), " // BigDecimal + "col_string VARCHAR(254), " // String + "col_date DATE, " // Date + "col_time TIME, " // Time + "col_timestamp TIMESTAMP, " // Timestamp + "col_asciistream TEXT, " // AsciiStream (< 2^16 bytes) + "col_binarystream LONGBLOB, " // BinaryStream (< 2^32 bytes) + "col_blob BLOB)"; // Blob (< 2^16 bytes) stmt.executeUpdate(sql); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:odbc:technical_library"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String theStatement = "SELECT authid, lastname, firstname, email FROM authors ORDER BY authid"; try {// w ww .j a v a 2 s . c om Class.forName(driver); Connection connection = DriverManager.getConnection(url, "guest", "guest"); Statement queryAuthors = connection.createStatement(); ResultSet results = queryAuthors.executeQuery(theStatement); String lastname, firstname, email; int id; while (results.next()) { id = results.getInt(1); lastname = results.getString(2); firstname = results.getString(3); email = results.getString(4); if (results.wasNull()) { email = "no email"; } System.out.println(Integer.toString(id) + ", " + lastname.trim() + ", " + firstname.trim() + ", " + email.trim()); } queryAuthors.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String userName = "suru"; String password = "password"; Connection con = DriverManager.getConnection(url, userName, password); System.out.println("Connection success!"); Statement stmt = con.createStatement(); String sql = "CREATE TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME VARCHAR2(50))"; stmt.execute(sql);//ww w . j a v a 2 s . c om System.out.println("Table created successfully!"); stmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; Class.forName(driverName).newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", userName, password);/*from w w w .ja v a 2 s .c o m*/ Statement st = con.createStatement(); st.executeUpdate("DROP TABLE Employee1"); con.close(); }