Java class file from PL/SQL procedure - java

I have a following PL/SQL procedure:
CREATE OR REPLACE PROCEDURE getDogInfo
(Dog_ID IN NUMBER, Dog_name OUT VARCHAR) AS
BEGIN
SELECT Dog_name INTO Name
FROM Dog_family
WHERE ID = Dog_ID;
END;
I need to make a java class file that does the same. I've been trying like this:
import java.sql.*;
import java.io.*;
public class Procedure {
public static void getDogInfo (int Dog_ID, String Dog_name)
throws SQLException
{ String sql =
"SELECT Dog_name INTO Name FROM Dog_family WHERE ID = Dog_ID";
try { Connection conn = DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement apstmt = conn.prepareStatement(sql);
apstmt.setInt(1, Dog_ID);
apstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
ResultSet rset = apstmt.executeQuery();
rset.close();
apstmt.close(); //Connection close
}
catch (SQLException e) {System.err.println(e.getMessage());
}
}
}
What am I doing wrong? Can someone help me get this working? Thanks

Have alook at this link showing you how to correctly use PreparedStatements.
You will find that the parameter should be ? not Dog_ID
Try
SELECT Name FROM Dog_family WHERE ID = ?
It will also show you how to iterate through your resultSet

Well, you do not tell us what the problem is, but I see several issue right away:
Your select statement should not have an INTO clause. That is a PL/SQL construct. You need to
return the result of the query back as a result set.
Your input parameter, Dog_ID will not be used, because you have not
named the parameter correctly in the SQL statement.
Java string parameters cannot be updated within the method, which I
am assuming that is what you are attempting. You either need to
return a string value from the method, or use a StringBuilder
reference, or some other container to pass in. See this link
There is no "out" parameter to register. Read up on result sets here
So, change your SQL statement to something like this(since you are using a positional parameter as opposed to a named parameter):
"SELECT Dog_name FROM Dog_family WHERE ID = ?"

You should read about JDBC (and Java in general too).
The query should be :
SELECT Name
FROM Dog_family
WHERE ID = ?
(assuming Name is the column name you are selecting from the table - it wasn't clear whether Name or Dog_name was the column name).
Then after you execute the query and get a result set :
String name = null;
if (rset.next()) {
name = rset.getInt (1);
}
...
return name;
Finally, your function should return a String. You can't pass the String as a parameter and update its value. String is immutable in Java.
One more thing - the line apstmt.registerOutParameter(2, java.sql.Types.VARCHAR); is not needed. registerOutParameter is only used with CallableStatement, which is a statement you use to execute a stored procedure.

Related

Prepared statement parameter bug for "JAVA parameter index out of range (1 > number of parameters, which is 0)"

I'm trying to insert a variable into the database.
Heres my code :
public static void main(String[] args) throws SQLException {
Connection connection =null;
DBhelper helper = new DBhelper();
PreparedStatement statement = null;
ResultSet resultSet;
try{
connection= helper.getConnection();
System.out.println("baglantı olustu");
String sql = "INSERT INTO pandemi(toplamvirus) VALUES ('?') ";
statement = connection.prepareStatement(sql);
statement.setInt(1,2);
statement.executeUpdate(sql);
int result = statement.executeUpdate();
}
catch (SQLException exception){
helper.showErrorMessage(exception);
}
finally {
statement.close();
connection.close();
}
//String sql = "UPDATE pandemi SET toplamvirus='ff' ";
}
And the error is:
baglantı olustu
Error: Parameter index out of range (1 > number of parameters, which is 0).
Error code : 0
The database is: https://prnt.sc/rile7q
Pass argument ordinal and a value to setInt
You are incorrectly using the PreparedStatement::setInt method. The first argument is an ordinal number (incorrectly documented in the Javadoc as an index). So first placeholder is # 1, second placeholder is # 2, and so on. You have only a single ? placeholder. Or were you trying to pass a value of two to be inserted into the database? Your question is not clear.
Use naked ?
Also, you need to remove the single-quote marks from around the ? placeholder in your prepared statement. Using '?' means you want a single-character string consisting of a question mark inserted into the database. With the single-quotes in place, you have no placeholder in your SQL statement. So your setInt method will fail. If using a naked ?, the question mark will be recognized as a placeholder.
By the way, I suggest making a habit of using the SQL statement terminator, a semicolon.
String sql = "INSERT INTO pandemi ( toplamvirus ) VALUES ( ? ) ; ";
For more info, see another Answer of mine with a complete example app using the H2 database engine demonstrating INSERT with a placeholder in a prepared statement.

Placeholders not working when using SQL statement in Java (JDBC) [duplicate]

This question already has answers here:
Variable column names using prepared statements
(7 answers)
Using Prepared Statements to set Table Name
(8 answers)
Closed 4 years ago.
Very first question here so I apologize for any mistakes and imperfections.
Basically there are three files, my main method tech_supportv1, login_controller containing a class used to store a bunch of methods, and login.java, a javabean.
The point is to check if a certain row exists on the tech_support database. To do so I'm trying to use the code below. (db_util and type are classes containing connection data, they are tested and they work).
ISSUE: the data from the main method seems not be pasted into the string in the appropriate placeholders, and an error is returned. (Of course if I manually enter the strings instead of using placeholders, everything works just fine.)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''technicians' WHERE 'techID' LIKE 16' at line 1
I tired to look at the mariaDB docs but all the other syntax provided yields the same results.
So this the main method.
public class Tech_support_v1 {
public static void main(String[] args) {
System.out.println("Start.");
login bean = new login();
bean.setTable("technicians");
bean.setColumn("techID");
bean.setID(16);
login_controller.select(bean);
}
}
This is the select method (with bean as argument, login is the Javabean class).
public static boolean select(login bean) {
String sql = "SELECT * FROM ? WHERE ? LIKE ?";
ResultSet rs;
try (
Connection conn = db_util.getConn(db_type.MYSQL);
PreparedStatement stmt = conn.prepareStatement(sql);
) {
stmt.setString(1, bean.getTable());
stmt.setString(2, bean.getColumn());
stmt.setInt(3, bean.getID());
rs = stmt.executeQuery();
if (rs.next()) {
System.out.println("Y");
return true;
} else {
System.err.println("N");
return false;
}
} catch (Exception e) {
System.err.println(e);
return false;
}
}
I won't include the bean class because it's literally only three variables with the relative set/get methods. Also the database runs with MariaDB, and is MySQL.
Thanks to everyone in advance.
You have multiple problems with your code :-)
First, you can't set table or column names with "setString" in a Prepared Statement!
See this Question: How to use a tablename variable for a java prepared statement insert
Second, as Daniel Pereira pointed out: You are trying to use a "like" Statement with "setInt"! See: https://dev.mysql.com/doc/refman/8.0/en/pattern-matching.html
You are comparing with LIKE but you are setting an Int. Change the LIKE to = and see if it works.
Try doing something like this:
String sql="SELECT * FROM :table ";
try (
Connection conn = db_util.getConn(db_type.MYSQL);
PreparedStatement stmt = conn.prepareStatement(sql);
)
{
String query = StringUtils.replace(sql, ":table", bean.getTable());
stmt.executeQuery(query);
}

Proper use of Stored Procedure using Select

I created a Stored Procedure where I can select the column that I address in my Stored Procedure via Callable Statement. I tried to use SELECT SECTION NAME FROM allsections_list WHERE SECTION_NAME = ? Similar to syntax of Prepared Statement but I think its not compatible using this syntax. I'm just new learning this mysql.
Stored Procedure
CREATE STORED PROCEDURE getSECTION_NAME(OUT SECTION_NAME VARCHAR)
SELECT SECTION_NAME FROM allsections_list
Code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();
String searchSection_Name = Section_SectionName_TextField.getText();
if (searchSection.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please fill up this fields");
}
else
try (Connection myConn = DBUtil.connect();
CallableStatement myCs = myConn.prepareCall("{call getSECTION_NAME(?)}"))
{
myCs.setString(1, searchSection_Name);
try (ResultSet myRs = myCs.executeQuery())
{
int resultsCounter = 0;
while (myRs.next())
{
String getSection_Name = myRs.getString(1);
Section_SectionName_TextField.setText(getSection_Name);
resultsCounter++;
}
}
}
catch (SQLException e)
{
DBUtil.processException(e);
}
When I search a records. If the records exist it the value will print out to the textfields. But it doesn't print out. And it throws me a error getSECTION_NAME does not exist. What if I want select multiple values? Because I'm having a project where I'm making a Enrollment System. I choose this Stored Procedure specially than Batch Statement based on what I read. Any help will appreciate. Thanks!
I don't use MySql, but here's a similar example in Oracle (I think this Works in MySql as well).
CREATE PROCEDURE get_section_name(OUT secName VARCHAR(100))
BEGIN
SELECT SECTION_NAME INTO secName FROM allsections_list WHERE some_condition = 100; //your procedure does not use any input arguments if you want to return just one record then you'll probably need to specify a where clause
END
/ //when executing a stored procedure in a DB client you will need to specify a terminator character (in this case slash /)
Note that there's no return statement because we're using OUT parameters.
The getOutValueForStoredProcedure method calls the stored procedure and retrieves the out value.
public String getOutValueForStoredProcedure(String procedureName, int sqlType) throws EasyORMException{
String out=null;
CallableStatement stmt=null;
try{
//out parameters must me marked with question marks just as input parameters
sqlQuery = "{call " + procedureName +"(?)}";
stmt=conn.prepareCall(sqlQuery);//I assume that a Connection has been created
stmt.registerOutParameter(1, sqlType);
stmt.execute();
out = stmt.getString(1);//you get the out variable through the Statement, not the ResultSet
}catch(Exception e){
//log exception
}finally{
//close stmt
}
return out;
}
To call this stored procedure, you can use
String out = getOutValueForStoredProcedure("get_section_name", java.sql.Types.VARCHAR);
For creating stored procedures in MySql , check this link http://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843
For a more elaborate example, check this http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/

Sql query with bind variables execution in Jdbc

I have a sql query like this.
select "DEPT"."DEPTNO" as "DEPTNO1",
"DEPT"."DNAME" as "DNAME1",
"DEPT"."LOC" as "LOC1",
"EMP"."COMM" as "COMM1",
"EMP"."EMPNO" as "EMPNO1",
"EMP"."ENAME" as "ENAME1",
"EMP"."HIREDATE" as "HIREDATE1",
"EMP"."JOB" as "JOB1",
"EMP"."MGR" as "MGR1",
"EMP"."SAL" as "SAL1"
from "EMP" , "DEPT" where "DEPT"."DEPTNO" in (:DeptNo)
//This is the Jdbc code
Class.forName(DB_DRIVER);
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
Statement statment = dbConnection.createStatement();
result = statment.execute(query);//query is above sql query
When i run above query in Oracle sql developer works perfectly.But when i run it with above jdbc code it is throwing Not all variables bound exception.
How to run above query in JDBC
replace :deptno in your query with a ?.
and instead of instantiating statement use the following:
PreparedStatement stmt=con.prepareStatement(query);
stmt.setInt(1,deptno); //1 is for the first question mark
where deptno holds the value for which you want to execute the query.
Through PrepredStatement interface we can use parametrized query which is compiled only once and has performance advantage in comparison to the Statement interface.
You created a Query with bind variable and you never set it.
Use OraclePreparedStatement and its method setStringAtName()
statement.setStringAtName("DeptNo","<<your Value>>");
If not OraclePreparedStatement, you can just put it as ?1 in your Query string
and use,
statement.setString(1,"<<your Value>>");
If in case, you don't know how many bind variables you get, you have capture the bind variables in a map and prepare a list and set it accordingly!
Else your requirement is unachievable!
Get the query dynamically from the report.
From this query we need to split querystring to get number of bind variables and placing those bind variables in a HashMap.HashMap is like
{DeptName =1, Job =1, DeptNo =1}
From this hashmap,need to replace the query bind variable with ?.For this we need to do like
bindkey = entry1.getKey().toString();
String bindkeyreplace =":".concat(bindkey).trim();
String bindkeyreplacestring = "?";
query = query.replace(bindkeyreplace, bindkeyreplacestring);
Then we will get dynamic query coming from the report with ? instead of :bindvariable
PreparedStatement prestmt = dbConnection.prepareStatement(query);
for (int i = 0; i < bindParamMap.size(); i++) {
prestmt.setInt(i + 1, 0);//Setting default value to check the query is running successfully or not
}
result = prestmt.execute();
If in case, we don't know how many bind variables we get then this approach is running successfully for me.
Use this syntax,EMP.DNAME as DNAME1. I mean your dot and as must be inside the double quotes.
The variable DeptNo must be bound to a value before you execute the statement like below.
DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
Statement statment = dbConnection.createStatement();
//Bind deptno to a value
statment.setParameter("DeptNo",5);
result = statment.execute(query);
You must set values for all the variables in your prepared statement othwerise you cannot execute the statement. If you receive the query to execute itself as an input then you should also get the parameters and its values also as input. Something like below
public <returnType> executeQuery(String queryStr, Map<String,Object> params) {
//Code to create connecitno and statment from queryStr.
//Bind deptno to a value
for(int i=0;i<params.size(),i++) {
//Get entry set from map
statment.setParameter(entryset.getKey(),entryset.getValue());
}
result = statment.execute(query);
//return or work on the result
}

"Parameter type conflict" when calling Java Stored Procedure within another Java Stored Procedure

Here's the problem (sorry for the bad english):
i'm working with JDeveloper and Oracle10g, and i have a Java Stored Procedure that is calling another JSP like the code:
int sd = 0;
try {
CallableStatement clstAddRel = conn.prepareCall(" {call FC_RJS_INCLUIR_RELACAO_PRODCAT(?,?)} ");
clstAddRel.registerOutParameter(1, Types.INTEGER);
clstAddRel.setString(1, Integer.toString(id_produto_interno));
clstAddRel.setString(2, ac[i].toString());
clstAddRel.execute();
sd = clstAddRel.getInt(1);
} catch(SQLException e) {
String sqlTeste3 = "insert into ateste values (SQ_ATESTE.nextval, ?)";
PreparedStatement pstTeste3 = conn.prepareStatement(sqlTeste3);
pstTeste3.setString(1,"erro: "+e.getMessage()+ ac[i]);
pstTeste3.execute();
pstTeste3.close();
}
I'm recording the error in a table called ATESTE because this JavaSP is a procedure and not a function, I've to manipulate DML inside.
So, the error message I'm getting is: 'parameter type conflict'...
the function "FC_RJS_INCLUIR_RELACAO_PRODCAT" it's a Java Stored Procedure too, it's already exported to Oracle, and returns an int variable, and i have to read this to decide which webservice i will call from this JavaSP.
I have already tried the OracleTyep.NUMBER in the registerOutParameter.
Anyone knows what i'm doing wrong?
It looks like you are missing a parameter in your call. You register an Integer output parameter, and then you set 2 string parameters. I'm presuming your procedure FC_RJS_INCLUIR_RELACAO_PRODCAT returns an integer value. If so your code should look more like this:
CallableStatement clstAddRel = conn.prepareCall(" { ? = call FC_RJS_INCLUIR_RELACAO_PRODCAT(?,?)} ");
clstAddRel.registerOutParameter(1, Types.INTEGER);
clstAddRel.setString(2, Integer.toString(id_produto_interno));
clstAddRel.setString(3, ac[i].toString());
clstAddRel.execute();

Categories

Resources