如何将数据库中的数据调用到netbeans中的java类?

时间:2016-03-12 16:13:28

标签: java sql database netbeans derby

第一次发帖很抱歉,如果我的问题有点奇怪。

所以我在学校有一个项目要求我们使用netbeans创建java类,打开一个包含三个选项的窗口,检查库存,购买项目和更新库存。

我们有一个名为stockdata的课程,其中包含5个不同项目的详细信息,供我们在三个班级中使用,以检查,购买和更新项目。我们课程的最新阶段要求我们创建一个德比数据库并将项目输入表格。

我这样做没有任何问题,但我遇到问题从表中的项目回到我的类中使用。我们得到了以下代码,但即使使用注释提示,我也无法使其工作。

package stock;

// Skeleton version of StockData.java that links to a database.
// NOTE: You should not have to make any changes to the other
// Java GUI classes for this to work, if you complete it correctly.
// Indeed these classes shouldn't even need to be recompiled
import java.sql.*; // DB handling package
import java.io.*;
import org.apache.derby.drda.NetworkServerControl;

public class StockData {

    private static Connection connection;
    private static Statement stmt;

    static {
    // standard code to open a connection and statement to an Access         database
        try {
            NetworkServerControl server = new NetworkServerControl();
            server.start(null);
            // Load JDBC driver
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            //Establish a connection
            String sourceURL = "jdbc:derby://localhost:1527/"
                    + new File("UserDB").getAbsolutePath() + ";";
            connection = DriverManager.getConnection(sourceURL, "use",   "use");
            stmt = connection.createStatement();
    } // The following exceptions must be caught
    catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
    } catch (SQLException sqle) {
        System.out.println(sqle);
    } catch (Exception e) {
        System.out.println(e);
    }

}

// You could make methods getName, getPrice and getQuantity simpler by using an auxiliary
// private String method getField(String key, int fieldNo) to return the appropriate field as a String
public static String getName(String key) {
    try {
        // Need single quote marks ' around the key field in SQL. This is easy to get wrong!
        // For instance if key was "11" the SELECT statement would be:
        // SELECT * FROM Stock WHERE stockKey = '11'
        ResultSet res = stmt.executeQuery("SELECT * FROM Stock WHERE stockKey = '" + key + "'");
        if (res.next()) { // there is a result
            // the name field is the second one in the ResultSet
            // Note that with  ResultSet we count the fields starting from 1
            return res.getString(2);
        } else {
            return null;
        }
    } catch (SQLException e) {
        System.out.println(e);
        return null;
    }
}

public static double getPrice(String key) {
    // Similar to getName. If no result, return -1.0
    return 0;
}

public static int getQuantity(String key) {
    // Similar to getName. If no result, return -1

    return 0;
}

// update stock levels
// extra is +ve if adding stock
// extra is -ve if selling stock
public static void update(String key, int extra) {
    // SQL UPDATE statement required. For instance if extra is 5 and stockKey is "11" then updateStr is
    // UPDATE Stock SET stockQuantity = stockQuantity + 5 WHERE stockKey = '11'
    String updateStr = "UPDATE Stock SET stockQuantity = stockQuantity + " + extra + " WHERE stockKey = '" + key + "'";
    System.out.println(updateStr);
    try {
        stmt.executeUpdate(updateStr);
    } catch (SQLException e) {
        System.out.println(e);
    }
}

// close the database
public static void close() {
    try {
        connection.close();
    } catch (SQLException e) {
        // this shouldn't happen
        System.out.println(e);
    }
}

}

很抱歉,如果这似乎是一个愚蠢的问题,但我对Java很新,并且在这个障碍之前取得了很好的进展。

提前致谢!

Alex

2 个答案:

答案 0 :(得分:0)

搜索" java sql"在Google上发布此链接:https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

从连接中你可以创建一个语句(你可以在链接和你的代码中找到它),然后获取一个结果集并用rs.next()循环它。这应该是你的开始。

当然你必须确保驱动程序和数据库在那里/正在运行,只是说......

答案 1 :(得分:0)

这里的netbeans与数据库无关。这是一个基于Java的集成开发环境(IDE),可帮助您减少语法错误。

public void dataAccess(){

ObservableCollection<Item>