我应该做的是创建一个Java存储过程,然后从Java程序中调用它。
我在执行JAVA程序时遇到问题 - JDBCPiemers(下面)有1个参数(我需要输出String Nosaukums)。错误:“列名无效”。
调用ORACLE中的过程可以正常工作。
这是我在数据库中加载的存储过程。
import java.sql.*;
import java.io.*;
public class BeerBeer {
public static void getBreweryInfo (int Raz_ID)
throws SQLException
{ String sql =
"SELECT Nosaukums FROM Alus_razotaji WHERE ID = ?";//vaicajums
try { Connection conn = DriverManager.getConnection("jdbc:default:connection:"); // Izveidojam savienojumu ar datubāzi
PreparedStatement apstmt = conn.prepareStatement(sql);
apstmt.setInt(1, Raz_ID);
ResultSet rset = apstmt.executeQuery();// SQL vaicājuma izpildīšana
rset.close();
apstmt.close(); //Savienojuma aizvēršana
}
catch (SQLException e) {System.err.println(e.getMessage()); //Kļūdu izvadīsana
}
}
}
我用oracle创建了一个以下的包。
CREATE OR REPLACE PACKAGE BeerBeer AS
PROCEDURE getBreweryInfo(Raz_ID number);
END BeerBeer;
CREATE OR REPLACE PACKAGE BODY BeerBeer AS
PROCEDURE getBreweryInfo(Raz_ID number) AS Language Java
NAME 'BeerBeer.getBreweryInfo(int)';
END BeerBeer;
这是我的Java程序,我在其中调用了该过程。但是我无法让它发挥作用。我收到错误:“列索引无效”
package client;
import java.sql.*;
public class JDBCPiemers {
// Norādam URL draiverim un datu bāzei
static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
// Izveidojam norādi uz datu bāzes lietotājvārdu un paroli
static final String USER = "SYSTEM";
static final String PASS = "asdasd";
private String sql;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
CallableStatement stmt = null;
try {
//Reģistrējam JDBC draiveri
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Izveidojam savienojumu ar datubazi ...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Vaicājuma izveide
System.out.println("Komandas veidosana...");
String SQL = "{CALL BeerBeer.getBreweryInfo (?)}";
stmt = conn.prepareCall(SQL);
int Raz_ID = 4;
stmt.setInt(1, Raz_ID);
stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
System.out.println("Izpildam JAVA glabajamo proceduru ...");
// Vaicājuma izpilde
stmt.execute();
//Izgustam Alus_razotaja nosaukumu ar getXXX metodi.
String Razotajs = stmt.getString(2);
System.out.println("Izvadam rezultatus ...");
System.out.println("Identifikatoram NR. " +Raz_ID + " atbilst Alus_Razotajs ar nosaukumu '" + Razotajs + "'" );
stmt.close();
conn.close(); }
catch(SQLException se) {
//Kludu apstrade JDBC
se.printStackTrace(); }
catch(Exception e) {
//Kludu apstrade klasei Class.forName
e.printStackTrace(); }
finally {
// Bloks, lai aizvērtu resursus
try {if(stmt!=null)
stmt.close(); }
catch(SQLException se2) {}
try {if(conn!=null)
conn.close(); }
catch(SQLException se) {se.printStackTrace(); }
//finally bloka beigas
} // try
System.out.println("Darbu beidzam."); }}
答案 0 :(得分:0)
CallableStatement stmt = null;
String sql = "{call movecartrecord(?)}";
try
{
stmt = conn.prepareCall(sql);
stmt.setInt(1, order_id); //you can pass argument if procedure accepts
stmt.execute();
stmt.close();
String name = stmt.getString(1);
System.out.println("Name : " + name);
}
catch (SQLException e)
{
e.printStackTrace();
}
这里movecartrecord()是一个存储过程名称,conn是一个连接对象。
这里order_id作为参数传递。 如果您的存储过程没有任何参数,那么只需保留该行代码。
希望它会起作用并帮助你。
这是一个精彩的code,向您展示如何执行它。
答案 1 :(得分:0)
要返回一个值,您需要将它作为一个函数,而不是一个过程:
public static String getBreweryInfo (int Raz_ID)
throws SQLException
{
String sql =
"SELECT Nosaukums FROM Alus_razotaji WHERE ID = ?";//vaicajums
try {
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement apstmt = conn.prepareStatement(sql);
apstmt.setInt(1, Raz_ID);
ResultSet rset = apstmt.executeQuery();// SQL vaicājuma izpildīšana
if (rset.next()) {
return rset.getString(1);
}
}
catch (SQLException e) {
System.err.println(e.getMessage()); //Kļūdu izvadīsana
}
finally {
rset.close();
apstmt.close(); //Savienojuma aizvēršana
}
}
然后你的包裹变成:
CREATE OR REPLACE PACKAGE BODY BeerBeer AS
FUNCTION getBreweryInfo(Raz_ID number) RETURN varchar2 AS Language Java
NAME 'BeerBeer.getBreweryInfo(int) return java.lang.String';
END BeerBeer;
你称之为:
String SQL = "{CALL ? = BeerBeer.getBreweryInfo (?)}";
stmt = conn.prepareCall(SQL);
int Raz_ID = 4;
stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
stmt.setInt(2, Raz_ID);
System.out.println("Izpildam JAVA glabajamo proceduru ...");
// Vaicājuma izpilde
stmt.execute();
//Izgustam Alus_razotaja nosaukumu ar getXXX metodi.
String Razotajs = stmt.getString(1);
请注意,out参数现在是索引1,因为它是函数的返回值,因此传入的值现在是索引2.所有未经测试的当然......