简体   繁体   English

Derby中的存储过程

[英]Stored Procedures in Derby

I am new to Derby and to databases in general for that matter. 对于这件事,我是Derby和一般数据库的新手。 How can I create a prepared statment for an embedded derby database? 如何为嵌入式德比数据库创建准备好的语句? Im not sure because the database is embedded. 我不确定,因为数据库是嵌入式的。

My String is: 我的字符串是:


final String updateString = "create table " + TABLE_NAME_TBL_IPS + " (" +
                TABLE_COLUMN_COMPANY + " " + TABLE_COLUMN_COMPANY_DATA_TYPE+ "," +
                TABLE_COLUMN_IP + " " + TABLE_COLUMN_IP_DATA_TYPE + ")";

Also what is the benefit of using this as a stored procedure instead of a prepared statement call? 将其用作存储过程而不是准备好的语句调用还有什么好处?

It doesn't really matter if the database is embedded or not, as long as it has JDBC connectivity. 只要数据库具有JDBC连接,是否嵌入还是没有关系。 In your case, Derby does provide you to the connection information . 就您而言,Derby确实为您提供了连接信息

Your code may look something like this:- 您的代码可能看起来像这样:

// much easier to read with String.format()... in my opinion
final String updateString = String.format("create table %s (%s %s, %s %s)",
        TABLE_NAME_TBL_IPS,
        TABLE_COLUMN_COMPANY,
        TABLE_COLUMN_COMPANY_DATA_TYPE,
        TABLE_COLUMN_IP,
        TABLE_COLUMN_IP_DATA_TYPE);

Connection con = null;

try {
    con = DriverManager.getConnection("jdbc:derby:yourDatabaseName");

    PreparedStatement ps = con.prepareStatement(updateString);
    ps.executeUpdate();
    ps.close();
}
catch (SQLException e) {
    e.printStackTrace();
}
finally {
    try {
        con.close();
    }
    catch (Exception ignored) {
    }
}

Regarding your question whether to do so using a stored procedure or a PreparedStatement , there are bunch of information out there you are easily search. 关于您是使用存储过程还是PreparedStatement这样做的问题,您可以轻松搜索大量信息。 You generally use a stored procedure to group bunch of SQL statements whereas a PreparedStatement only allows you to execute one SQL statement. 通常,您使用存储过程对一堆SQL语句进行分组,而PreparedStatement仅允许您执行一个SQL语句。 It is a good idea to use stored procedures if you intend to expose that API to allow your users to execute it regardless of technology (Java, .NET, PHP). 如果您打算公开该API以允许您的用户不管技术(Java,.NET,PHP)如何执行该存储过程,则最好使用存储过程。 However, if you are writing this SQL statement only for your Java application to work, then it makes sense to just use PreparedStatement . 但是,如果只为使Java应用程序正常工作而编写此SQL语句,则仅使用PreparedStatement是有意义的。

既然您注意到了Derby和数据库的新手,那么这里有一些很好的资源: http: //download.oracle.com/javase/tutorial/jdbc/index.htmlhttp://db.apache。 org / derby / papers / DerbyTut /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM