English 中文(简体)
创建从 derby 数据库获取值的类
原标题:Create a class to get value from the derby database

我创建了一个要连接到 derby 数据库的类。 在程序里, 我创建了表格, 并插入了一些水果名称作为值 。 我只是想知道我如何能够创建另一个类, 在新的阵列中获取水果名称值 。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class FruitDatabase {

    public static void main(String[] args) throws Exception {
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();

        stmt.executeUpdate("drop table fruit");
        stmt.executeUpdate("create table fruit (name CHAR(15))"); 
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Apple )"); 
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Orange )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Banana )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Pineapple )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Mango )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Kiwi )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Grape )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Strawberry )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Pear )");
        stmt.executeUpdate("INSERT INTO fruit(name)VALUES( Peach )");
        ResultSet rs = stmt.executeQuery("SELECT * FROM fruit"); 
        outputResultSet(rs);

        rs.close();
        stmt.close();
        conn.close();
    }

    private static void outputResultSet(ResultSet rs) throws Exception {
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int numberOfColumns = rsMetaData.getColumnCount();
        for (int i = 1; i < numberOfColumns + 1; i++) {
            String columnName = rsMetaData.getColumnName(i);
            System.out.print(columnName + "   ");

        }
        System.out.println();
        System.out.println("----------------------");

        String array [] = new String [numberOfColumns + 1];

        while (rs.next()) {
            for (int i = 1; i < numberOfColumns + 1; i++) {
                array[i] = rs.getString(i);
                System.out.println(array[i]);
            }

        }

    }

    private static Connection getConnection() throws Exception {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        String url = "jdbc:derby:MyDbTest;create=true";
        return DriverManager.getConnection(url);
    }

}
问题回答

将您宣布为私有变量的 < code> 数组 < / code> 设定为私有变量, 初始化为静态块, 并添加选取和设置方法 。 这里没有使用选取器, 但这只为您理解 。

FruitDatabase.java

public class FruitDatabase {
    private static String[] array;

    public static String[] getArray() {
        return array;
    }

    public static void setArray(String[] arr) {
        array = arr;
    }

    public static void main(String[] args) throws Exception {
    ...
    }

    private static void outputResultSet(ResultSet rs) throws Exception {
    ... 
    while (rs.next()) {
            for (int i = 1; i < numberOfColumns + 1; i++) {
                array[i] = rs.getString(i);
                System.out.println(array[i]);
            }
    setArray(array);
    ....
    }
    ....
}

然后从其他类( 假设显示. java) 中, 您需要创建此类的对象, 然后您就可以访问此数组 :

Display.java

public class Display {
    public static void main(String[] args) {
        String[] newArr = new String[FruitDatabase.getArray().length];
    int i=0;
    for(String s : FruitDatabase.getArray()){
        System.out.println(s);
            newArr[i++]=s;
    }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签