如何在Oracle中使用Java存储过程
1个回答
2016-08-07 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询