SQL查询Java网络bean
基本上,我有一个表名java_db.Customer,其中包含CustomerID列, 姓名、地址和财务OK 基本上,我想写一个简单的查询,显示FinanceOK=true JOptionPane.ShowMessage“Finance Accepted” 否则,FinanceOK=False JOptionPane.ShowMessage“Finance谢绝”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
编写此查询的最佳方式是什么?现在,您的帖子中有很多信息缺失,但内容不完整,下面是您如何完成此任务的方法:
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()
发送参数值。