实例代码参考如下:
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);
}
}
}
查询表
bankMoney
的内容的存储过程
create procedure sp_query_bankMoney
as
select * from bankMoney
go
exec sp_query_bankMoney
注* 在使用过程中只需要把中的SQL语句替换为存储过程名,就可以了很方便吧
以下是我的一个示例,有问题直接问:
用于打印本机操作系统目录的java存储过程。
--1. java source
create or replace and compile java source named listlirectory as
import java.io.File;
public class ListDirectory {
public static String returnFileList (String directory){
String fileList = "" ;
String[] files ;
File file = new File(directory) ;
files = file.list() ;
for (int i=1 ; i < files.length ; i++ ){
fileList = fileList+"&"+files[i]; ;
}
fileList = fileList.substring(1,fileList.length());
return fileList ;
}
}
--2. function
create or replace function getfiles(directory varchar2) return varchar2 as
language java name 'ListDirectory.returnFileList(java.lang.String) return java.lang.String';
--3. call
SQL> exec dbms_output.put_line(getfiles('d:\')) ;
1.txt&2.txt&3.txt&4.txt&4DC974E59D1D30746ABAFECB4CE7D8D9FDF1B7D7.torrent&5.txt&afiedt.buf&apps&baidu player&Beach House - S01E01 - Steel Panther.avi&Beach House - S01E02 - Lil Jon.avi&beach_house_party_720&beach_house_vanessa_robbins 2&CTL.CTL&CTL2.CTL&CTL3.CTL&CTL4.CTL&dex&dex.ora&dexSuite&flatfile&kankan&KSafeRecycle&ldrctl.ctl&ldrctl.log&ldr_text&loader.dat&LOL_V3.0.6.1_FULL.exe&msdia80.dll&msdownld.tmp&MSOCache&MyDrivers&ppp.ora&RMAN1.SET&RMAN2.SET&RMAN3.SET&RMAN4.SET&softstep&solaris.docx&StormMedia&System Volume Information&tcp-ip协议详解.pdf&TDDOWNLOAD&temp&vimtemp&workspace&美语正音&谁来陪我过圣诞_Surviving.Christmas_2004_DVD.rmvb
PL/SQL procedure successfully completed