Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询Java网络bean_Java_Sql_Jdbc - Fatal编程技术网

SQL查询Java网络bean

SQL查询Java网络bean,java,sql,jdbc,Java,Sql,Jdbc,基本上,我有一个表名java_db.Customer,其中包含CustomerID列, 姓名、地址和财务OK 基本上,我想写一个简单的查询,显示FinanceOK=true JOptionPane.ShowMessage“Finance Accepted” 否则,FinanceOK=False JOptionPane.ShowMessage“Finance谢绝” 编写此查询的最佳方式是什么?现在,您的帖子中有很多信息缺失,但内容不完整,下面是您如何完成此任务的方法: long customerI

基本上,我有一个表名java_db.Customer,其中包含CustomerID列, 姓名、地址和财务OK

基本上,我想写一个简单的查询,显示FinanceOK=true JOptionPane.ShowMessage“Finance Accepted”

否则,FinanceOK=False JOptionPane.ShowMessage“Finance谢绝”


编写此查询的最佳方式是什么?

现在,您的帖子中有很多信息缺失,但内容不完整,下面是您如何完成此任务的方法:

long customerID_Variable = 232;   // .... whatever you have to provide customer ID number ....
String customerName = "John Doe"; // .... whatever you have to provide customer name ....
boolean financeOK = false;   // default financing approval flag
String message = "Finance Declined!"; //default message
        
// Catch SQLException if any...
try { 
    // Use whatever your connection string might be 
    // to connect to your particular Database....
    Connection conn = DriverManager.getConnection("jdbc:derby:c:/databases/salesdb jdbc:derby:salesdb");
    conn.setAutoCommit(false);
            
    // The SQL query string...
    String sql = "SELECT FinanceOK FROM Customer WHERE CustomerID = " + customerID_Variable + ";";
    // Although it is best to use the Customer ID  to reference a 
    // particular Customer you may prefer to to use the Customer
    // name and if so then use the query string below instead...
    // String sql = "SELECT FinanceOK FROM Customer WHERE Name = '" + customerName + "';";
            
    // Execute the SQL query...
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
            
    //Retrieve the data from the query result set...
    while (rs.next()) { 
        financeOK = rs.getBoolean("FinanceOK"); 
    }
            
    //Close everything...
    rs.close();
    stmt.close();
    conn.close();
} 
catch (SQLException ex) { ex.printStackTrace(); }
int msgIcon = JOptionPane.ERROR_MESSAGE;
if (financeOK) { 
    message = "Finance Accepted!"; 
    msgIcon = JOptionPane.INFORMATION_MESSAGE;
}
JOptionPane.showMessageDialog(null, message, "Financing Approval...", msgIcon);

建议将参数值连接到SQL字符串中是一个非常非常糟糕的建议。使用占位符
和例如
stmt.setString()
发送参数值。