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:com.vaadin.tutorial.todomvc.TodoModel.java
private static void setupDatabase(Connection connection) { try (Statement s = connection.createStatement()) { s.execute("CREATE TABLE todo (id INTEGER IDENTITY PRIMARY KEY, text VARCHAR(255) , completed BOOLEAN)"); } catch (SQLException ignored) { // Nothing to do here, because // the table already exists, re-creation failed }// w w w. j a v a2s. c om }
From source file:dbutils.ExampleJDBC.java
/** * Unlike some other classes in DbUtils, this class(SqlNullCheckedResultSet) is NOT thread-safe. *//*from ww w . ja v a2 s . co m*/ public static void findUseSqlNullCheckedResultSet() { Connection conn = getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, name, gender, age, team_id as teamId FROM test_student"); SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs); wrapper.setNullString("N/A"); // Set null string rs = ProxyFactory.instance().createResultSet(wrapper); while (rs.next()) { System.out.println("id=" + rs.getInt("id") + " username=" + rs.getString("name") + " gender=" + rs.getString("gender")); } rs.close(); } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:CheckJDBCInstallation_MySQL.java
/** * Test Validity of a Connection/*from ww w . j a va 2 s. c o m*/ * * @param conn * a JDBC connection object * @param query * a sql query to test against database connection * @return true if a given connection object is a valid one; otherwise return * false. */ public static boolean testConnection(Connection conn, String query) { ResultSet rs = null; Statement stmt = null; try { stmt = conn.createStatement(); if (stmt == null) { return false; } rs = stmt.executeQuery(query); if (rs == null) { return false; } if (rs.next()) { // connection object is valid: we were able to // connect to the database and return something useful. return true; } // there is no hope any more for the validity // of the connection object return false; } catch (Exception e) { // // something went wrong: connection is bad // return false; } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception e) { } } }
From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java
public static void relatoriosVendas() throws SQLException, ClassNotFoundException { String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM FILIAL FL " + "INNER JOIN FUNCIONARIO FUNC ON FUNC.ID_FILIAL = FL.ID_FILIAL " + "INNER JOIN ALUGUEL AL ON AL.ID_FUNCIONARIO = FUNC.ID_FUNCIONARIO " + "GROUP BY FL.NOME_FILIAL"; Connection conn = Conexoes.obterConexao(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); DefaultCategoryDataset ds = new DefaultCategoryDataset(); while (rs.next()) { ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL")); }// w w w . ja va 2 s.c o m JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Aluguis", "Filiais", "Separadas por filiais", ds, PlotOrientation.VERTICAL, true, true, false); try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\vendas.png")) { ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600); } catch (FileNotFoundException ex) { System.out.println("" + ex.getMessage()); } catch (IOException ex) { System.out.println("" + ex.getMessage()); } //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) { try (OutputStream arquivo = new FileOutputStream( "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) { ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600); } catch (FileNotFoundException ex) { System.out.println("" + ex.getMessage()); } catch (IOException ex) { System.out.println("" + ex.getMessage()); } }
From source file:com.example.mydtapp.JdbcInputAppTest.java
@BeforeClass public static void setup() { try {// w w w . j a v a 2 s . co m cleanup(); } catch (Exception e) { throw new RuntimeException(e); } try { Class.forName(DB_DRIVER).newInstance(); Connection con = DriverManager.getConnection(URL); Statement stmt = con.createStatement(); String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ACCOUNT_NO INTEGER, NAME VARCHAR(255),AMOUNT INTEGER)"; stmt.executeUpdate(createTable); cleanTable(); insertEventsInTable(10, 0); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java
public static void relatoriosDisponibilidade() throws SQLException, ClassNotFoundException { String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM CARRO CA " + "INNER JOIN FILIAL FL ON FL.ID_FILIAL = CA.ID_FILIAL " + "WHERE CA.DISPONIBILIDADE_CARRO = '1' " + "GROUP BY FL.NOME_FILIAL"; Connection conn = Conexoes.obterConexao(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); DefaultCategoryDataset ds = new DefaultCategoryDataset(); while (rs.next()) { ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL")); }/*from www .j a v a 2s. c om*/ /*File fg = new File("C:\\Users\\bruno.lopes.KRONMED\\Documents\\NetBeansProjects\\LoCarSys\\src\\main\\webapp\\ImagensLoCarSys\\disponibilidade.png"); fg.delete();*/ JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Disponibilidade", "Filiais", "Separados por filiais", ds, PlotOrientation.VERTICAL, true, true, false); //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) { try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\disponibilidade.png")) { ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600); } catch (FileNotFoundException ex) { System.out.println("" + ex.getMessage()); } catch (IOException ex) { System.out.println("" + ex.getMessage()); } //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) { try (OutputStream arquivo = new FileOutputStream( "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) { ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600); } catch (FileNotFoundException ex) { System.out.println("" + ex.getMessage()); } catch (IOException ex) { System.out.println("" + ex.getMessage()); } }
From source file:com.qubole.quark.server.EndToEndTest.java
public static void setupTables(String dbUrl, String filename) throws ClassNotFoundException, SQLException, IOException, URISyntaxException { Class.forName("org.h2.Driver"); Properties props = new Properties(); props.setProperty("user", "sa"); props.setProperty("password", ""); Connection connection = DriverManager.getConnection(dbUrl, props); Statement stmt = connection.createStatement(); java.net.URL url = EndToEndTest.class.getResource("/" + filename); java.nio.file.Path resPath = java.nio.file.Paths.get(url.toURI()); String sql = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8"); stmt.execute(sql);/*from w w w.ja v a2s. c o m*/ }
From source file:at.molindo.dbcopy.util.Utils.java
public static <T> T execute(Connection c, String query, ResultSetHandler<T> handler) throws SQLException { Statement stmt = c.createStatement(); try {/*from w w w .ja v a 2 s . c om*/ ResultSet rs = stmt.executeQuery(query); return handle(rs, handler); } finally { stmt.close(); } }
From source file:com.aurel.track.dbase.Migrate416To417.java
/** * Add the document and document section * Use JDBC because negative objectIDs should be added *///from w w w. ja va 2 s .c o m public static void addDeletedBasket() { TBasketBean basketBean = BasketBL.getBasketByID(TBasketBean.BASKET_TYPES.DELETED); if (basketBean == null) { LOGGER.info("Add 'Deleted basket' basket"); Connection cono = null; try { cono = InitDatabase.getConnection(); Statement ostmt = cono.createStatement(); cono.setAutoCommit(false); String deletedBasketStmt = addDeletedBasketStmt(TBasketBean.BASKET_TYPES.DELETED, "basket.label.-1", "-1001"); ostmt.executeUpdate(deletedBasketStmt); cono.commit(); cono.setAutoCommit(true); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } finally { try { if (cono != null) { cono.close(); } } catch (Exception e) { LOGGER.info("Closing the connection failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:com.p6spy.engine.spy.P6TestUtil.java
public static void execute(Connection con, String sql) throws SQLException { Statement stmt = null;/*from w w w .j av a2 s . c o m*/ try { stmt = con.createStatement(); stmt.execute(sql); } finally { if (stmt != null) try { stmt.close(); } catch (Exception e) { } } }