Java结束语

时间:2015-10-09 20:13:41

标签: sql jdbc execute

我不确定,哪种方法更好?

con.createStatement().execute("...");

OR

Statement st = con.createStatement();
st.execute("...");
st.close();

使用第一种方法时,语句是创建关闭还是保持打开状态?我确信在第二种方法中,语句将被关闭,但在第一种方法中?有人知道吗?

4 个答案:

答案 0 :(得分:2)

两者都不好,虽然第二种选择比第一种更好。完成后你应该总是关闭一个声明(不是很快,但绝对不是永远)。您的第二个替代方法会尝试关闭该语句,但如果public class SqlHelper { String title; String description; String image; private static final String TEXT_TYPE = " TEXT"; private static final String COMMA_SEP = ","; public static final String KEY_ROWID = "_id"; public static final String KEY_TITLE = "title"; public static final String KEY_DESCRIPTION = "description"; public static final String KEY_IMAGE = "image"; private SqlHelper mDb; private static final String DATABASE_NAME = "DBCategory"; private static final String DATABASE_TABLE = "categoryTable"; private static final int DATABASE_VERSION = 1; private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_TITLE + TEXT_TYPE + COMMA_SEP + KEY_DESCRIPTION + TEXT_TYPE + COMMA_SEP + KEY_IMAGE + TEXT_TYPE + " )"; private DbHelper ourHelper; private final Context ourContext; private SQLiteDatabase ourDatabase; public SqlHelper(Context c) throws SQLException { ourContext = c; try { open(); } catch (SQLException e) { e.printStackTrace(); } } public SqlHelper open() throws SQLException{ ourHelper = new DbHelper(ourContext); ourDatabase = ourHelper.getWritableDatabase(); return this; } public void close(){ ourHelper.close(); } private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TITLE + " TEXT NOT NULL," + KEY_DESCRIPTION + " TEXT NOT NULL," + KEY_IMAGE + " TEXT NOT NULL );" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } } public long createEntry(String title, String description,String image) { ContentValues cv = new ContentValues(); cv.put(KEY_TITLE, title); cv.put(KEY_DESCRIPTION,description); cv.put(KEY_IMAGE, image); return ourDatabase.insert(DATABASE_TABLE, null, cv); } public ArrayList<HashMap<String, String>> getAllData() { ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>(); //hp = new HashMap(); SQLiteDatabase db = this.ourHelper.getReadableDatabase(); Cursor res = db.rawQuery( "select * from categoryTable", null ); res.moveToFirst(); while(res.isAfterLast() == false){ HashMap<String,String> hashmap = new HashMap<String, String>(); hashmap.put("title", res.getString(res.getColumnIndex(KEY_TITLE))); hashmap.put("description", res.getString(res.getColumnIndex(KEY_IMAGE))); hashmap.put("image", res.getString(res.getColumnIndex(KEY_DESCRIPTION))); array_list.add(hashmap); res.moveToNext(); } return array_list; } } 抛出异常,则实际上无法执行此操作。

要保证关闭语句,可以使用try-with-resourc e(在Java 7中引入):

execute

如果你不幸被卡在Java 6或更早版本上,你需要使用try / finally:

try (Statement statement = connection.createStatement) {
    statement.execute();
    // Other things you want to with statement...
}

答案 1 :(得分:1)

如果您没有明确关闭Statement对象,那么它至少在最初时将保持打开状态。在垃圾收集器确定无法访问之后某个时间可能关闭,如果发生这种情况。当连接关闭时,关闭,如果那时它仍处于打开状态。

最好的形式通常是:

Statement st = con.createStatement();

try {
    // ...
    st.execute("...");
    // ...
} finally {
    st.close();
}

通过在创建语句后立即启动try块,可以确保语句在超出范围之前将被关闭(在finally块中) - 即使是任意的异常被抛到了内部。

答案 2 :(得分:0)

在第一个片段中,您没有明确地关闭该语句,因此您不能依赖它被关闭。某些驱动程序的实现在语句的ng-app="crm.ma"方法中调用close(),但您不应该依赖于此,并且应该始终显式关闭语句。

答案 3 :(得分:0)

con.createStatement().execute("...");Statement st = con.createStatement();都有同感。但是,不使用第一种方法执行关闭语句。关闭ResultSetStatement是一种很好的做法。未关闭Statements可能会遇到其他问题。您可以关闭Connection,但使用连接池时无法关闭。此外,第二种方法在使用PrepairedStatement时非常有用,因为它可以将参数设置为PreparedStatement