Java JDBC Sqlite Connection modify(String sql)

Here you can find the source of modify(String sql)

Description

modify

License

Open Source License

Declaration

public static void modify(String sql) 

Method Source Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import java.sql.Statement;

public class Main {
    private static String default_db_name = "data";

    public static void modify(String sql) {
        Connection conn = getConnection(null);
        Statement st = null;/*from w  w w. j  a  v a2  s.c  o  m*/
        try {
            st = conn.createStatement();
            st.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, st, null);
        }
    }

    public static void modify(String[] sqls) {
        Connection conn = getConnection(null);
        Statement st = null;
        try {
            if (sqls != null) {
                conn.setAutoCommit(false);
                st = conn.createStatement();
                for (String sql : sqls) {
                    st.addBatch(sql);
                }
                st.executeBatch();
                conn.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, st, null);
        }
    }

    public static Connection getConnection(String dbFilePath) {
        Connection conn = null;
        try {
            if (dbFilePath == null || "".equals(dbFilePath.trim())) {
                dbFilePath = default_db_name;
            }
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static void close(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. getConnection(String dbName)
  2. getNewConnection(File dbFile)
  3. getSenatorProp(String topicId)
  4. getSQLiteConnection()
  5. getUniqLabels()
  6. query(String sql)
  7. removeLabel()
  8. runSetupScript(final String setupScriptPath)