如何在Oracle中使用Java存储过程
2016-01-13 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
方法如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Load JDBC Driver
* 最基本的方法通过JDBC连接数据库
* @author Jacob
*
*/
public class LoadByPrimary {
public static void main(String[] args) throws ClassNotFoundException
{
String driver = "oracle.jdbc.OracleDriver";
Connection cn = null;
/**
* Class.forName手动加载一个类到方法区,Driver类中包含自动注册驱动的静态代码块
* 会自动在DriverManager中注册驱动
*/
Class.forName(driver);
String url = "jdbc:oracle:thin:@localhost:1521:ORACLE"; //1521代表端口号,默认的
String user = "用户名";
String pwd = "密码";
try
{
/*
* Connection是接口,返回值是一个引用对象,是Oracle驱动提供实现类ojdbc7.jar
* 使用JDBC API接口,实际上是驱动实现类
*/
cn = DriverManager.getConnection(url,user,pwd);
Statement stmt = cn.createStatement();
String sql = "SELECT * FROM stu_emp WHERE deptno =10";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt("empno")+" "+
rs.getString("ename")+" "+
rs.getString("job"));
}
rs.close();
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(cn!=null)
{
cn.close();
}
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
}
}
这是通过preparedstatement实现更新数据,这里我把连接数据库的方法进行了封装,每次直接调用了。
public static void main(String[] args)
{
PreparedStatementDemo psd = new PreparedStatementDemo();
psd.updateSalary("JACOB",3000);
psd.selectSalary("JACOB");
}
public void updateSalary(String ename,double sal)
{
String sql = "Update stu_emp set sal= ? Where ename = ?";
Connection cn = null;
PreparedStatement ps = null;
try
{
cn = DBPUtil.getConnection();
ps = cn.prepareStatement(sql);
ps.setDouble(1, sal);
ps.setString(2, ename);
int num = ps.executeUpdate();
System.out.println("提示:总共有 "+num+" 条数据已经更新!");
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.stmtClose(ps);
DBUtil.connClose(cn);
}
}
public void selectSalary(String name)
{
String sql = "Select * From stu_emp Where ename = ?";
Connection cn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
cn = DBPUtil.getConnection();
ps = cn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("ename")+" 的工资是: "+ rs.getInt("sal"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.stmtClose(ps);
DBUtil.rsClose(rs);
DBUtil.connClose(cn);
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Load JDBC Driver
* 最基本的方法通过JDBC连接数据库
* @author Jacob
*
*/
public class LoadByPrimary {
public static void main(String[] args) throws ClassNotFoundException
{
String driver = "oracle.jdbc.OracleDriver";
Connection cn = null;
/**
* Class.forName手动加载一个类到方法区,Driver类中包含自动注册驱动的静态代码块
* 会自动在DriverManager中注册驱动
*/
Class.forName(driver);
String url = "jdbc:oracle:thin:@localhost:1521:ORACLE"; //1521代表端口号,默认的
String user = "用户名";
String pwd = "密码";
try
{
/*
* Connection是接口,返回值是一个引用对象,是Oracle驱动提供实现类ojdbc7.jar
* 使用JDBC API接口,实际上是驱动实现类
*/
cn = DriverManager.getConnection(url,user,pwd);
Statement stmt = cn.createStatement();
String sql = "SELECT * FROM stu_emp WHERE deptno =10";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt("empno")+" "+
rs.getString("ename")+" "+
rs.getString("job"));
}
rs.close();
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(cn!=null)
{
cn.close();
}
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
}
}
这是通过preparedstatement实现更新数据,这里我把连接数据库的方法进行了封装,每次直接调用了。
public static void main(String[] args)
{
PreparedStatementDemo psd = new PreparedStatementDemo();
psd.updateSalary("JACOB",3000);
psd.selectSalary("JACOB");
}
public void updateSalary(String ename,double sal)
{
String sql = "Update stu_emp set sal= ? Where ename = ?";
Connection cn = null;
PreparedStatement ps = null;
try
{
cn = DBPUtil.getConnection();
ps = cn.prepareStatement(sql);
ps.setDouble(1, sal);
ps.setString(2, ename);
int num = ps.executeUpdate();
System.out.println("提示:总共有 "+num+" 条数据已经更新!");
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.stmtClose(ps);
DBUtil.connClose(cn);
}
}
public void selectSalary(String name)
{
String sql = "Select * From stu_emp Where ename = ?";
Connection cn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
cn = DBPUtil.getConnection();
ps = cn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("ename")+" 的工资是: "+ rs.getInt("sal"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.stmtClose(ps);
DBUtil.rsClose(rs);
DBUtil.connClose(cn);
}
}
}
展开全部
如何在java中使用oracle的存储过程吧?使用Jdbc不是有对应的调用方法吗?
下面是网上的例子代码:伪代码
Connection con = null;
CallableStatement cstmt = con.prepareCall("CALL exampleJDBC (?, ?, ?, ?, ?)");
cstmt.setString (1, “BeiJing”); // Set input parameter
cstmt.setInt (2, 2008); // Set input parameter
...
cstmt.executeUpdate();
int goldnumber = cstmt.getInt(3);
int silvernumber = cstmt.getInt(4);
String errorinfo = cstmt.getString(5);
cstmt.close();
下面是网上的例子代码:伪代码
Connection con = null;
CallableStatement cstmt = con.prepareCall("CALL exampleJDBC (?, ?, ?, ?, ?)");
cstmt.setString (1, “BeiJing”); // Set input parameter
cstmt.setInt (2, 2008); // Set input parameter
...
cstmt.executeUpdate();
int goldnumber = cstmt.getInt(3);
int silvernumber = cstmt.getInt(4);
String errorinfo = cstmt.getString(5);
cstmt.close();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询