How can I retrieve (SELECT) all my data using Stored Procedure while using this Stored Procedure via Callable Statement? I created a sample project where I need to SELECT all the data from my table. First I used a Prepared Statement so I can insert data to the following textfields. Btw I'm using derby.
TABLE
CREATE TABLE SAMPLEONLY
(
FULLNAME VARCHAR(50),
ADDRESS VARCHAR(50)
)
GUI
CODES FOR INSERT
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
String addFullname = fullnameTf.getText();
String addAddress = addressTf.getText();
String insert = "INSERT INTO SAMPLEONLY (FULLNAME,ADDRESS) VALUES (?,?)";
try (Connection myConn = DriverManager.getConnection(url, user, pass);
PreparedStatement myPs = myConn.prepareStatement(insert);){
myPs.setString(1, addFullname);
myPs.setString(2, addAddress);
myPs.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
After inserting to the following textfields. I need to retrieve all my data using Stored Procedure. I created a stored procedure base on what I read on Derby Reference Manual. How can insert SELECT * FROM SAMPLEONLY in my Stored Procedure?
STORED PROCEDURE
CREATE PROCEDURE show_data(IN FULLNAME VARCHAR(50), IN ADDRESS VARCHAR(50))
PARAMETER STYLE JAVA
LANGUAGE JAVA
READS SQL DATA
EXTERNAL NAME 'Frame.searchButton'
//HOW CAN I INSERT HERE?
How can I insert the SELECT * FROM SAMPLEONLY here? I'm a little bit confused creating this stored procedure because this is my first time to use this. I named my class as Frame
CODE FOR SELECT
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String seeachStoredProc = "{call show_data(?,?)}";
try (Connection myConn = DriverManager.getConnection(url, user, pass);
CallableStatement myCs = myConn.prepareCall(seeachStoredProc);)
{
//what goes here?
}
catch (SQLException e)
{
e.printStackTrace();
}
}
Any help will appreciate! Guide me if something wrong in my codes. Feel free to comment.
Please use below code
Stored procedure
CREATE PROCEDURE show_data(IN FULLNAME VARCHAR(50), IN ADDRESS VARCHAR(50))
PARAMETER STYLE JAVA
LANGUAGE JAVA
READS SQL DATA
EXTERNAL NAME 'Frame.searchButton'
BEGIN
INSERT INTO SAMPLEONLY("FULLNAME", "ADDRESS")
VALUES (FULLNAME , ADDRESS);
COMMIT;
END;
CODE FOR SELECT
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String seeachStoredProc = "{call show_data(?,?)}";
try (Connection myConn = DriverManager.getConnection(url, user, pass);
CallableStatement myCs = myConn.prepareCall(seeachStoredProc);)
{
String addFullname = fullnameTf.getText();
String addAddress = addressTf.getText();
myCs .setInt(1, addFullname
myCs .setString(2, addAddress );
// execute store procedure
myCs .executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
You could use something like the following:
CREATE PROCEDURE show_data(IN FULLNAME VARCHAR(50), IN ADDRESS VARCHAR(50))
PARAMETER STYLE JAVA
LANGUAGE JAVA
READS SQL DATA
DYNAMIC RESULT SETS 1
EXTERNAL NAME 'SomeClass.showData'
Inside the SomeClass class (or the forms class) add an static method called showData that has two string parameters
public class SomeClass{
public static void showData(String fullname, String address, ResultSet[] data){
Connection conn = ... //somehow create a connection to database
PreparedStatement ps = conn.prepareStatement("SELECT * FROM SAMPLEONLY");
data[0] = ps.executeQuery();
conn.close();
}
}
And inside your button
CallableStatement cs = conn.prepareCall("{ call showData(?, ?)}");
cs.setString(1, p1);
cs.setString(2, p2);
boolean hasResults = cs.execute();
if (hasResults) {
ResultSet rs = cs.getResultSet();
while (rs.next()) {
// do stuff with selected data
}
}
EDIT I've noticed that when you access to the connection in the stored procedure, you must use the connection string jdbc:default:connection So in the code above change the connection line
public class SomeClass{
public static void showData(String fullname, String address, ResultSet[] data){
Connection conn = DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement("SELECT * FROM SAMPLEONLY");
data[0] = ps.executeQuery();
conn.close();
}
In derby stored procedures you always return a result in form of another method parameter (in this case the ResultSet array). The NullException you're having is probably somewhere else }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.