如何使用java对oracle数据库进行增删查改

如何使用java对oracle数据库进行增删查改,第1张

用eclipse

首先在oracle文件下找到jdbc文件,里面的lib文件下有个class12zip

在eclipse里新建个项目,建好后在左边右击那个项目图标,选择属性(最下面的),里面有个java build path,点进去,找到add external jars按键,点一下,把那个class12zip加载进来,然后可以开始写代码了。

我的代码如下:

import javasql;

public class connect {

/

@param args

/

create or replace procedure sp_pro7

(spBookID in number,spkookName in varchar2, sppublish in varchar2) is

begin

insert into book values(spBookID,spkookName,sppublish);

commit;

end;

insert into 后要提交的,加个commit;吧

日期格式应该是Timestamp类型,是因为你打印出来才会显示最后的零的,直接查询出来,再存入另一张表就OK,不用转换

注:如果想打印出来的没有最后的零,可以用

public static final SimpleDateFormat dtmFormat = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss"); // 设置格式化类型

Time

Timestamp dtm = new Timestamp(SystemcurrentTimeMillis()); // 当前时间

String dateStr = dtmFormatformat(dtm); // 把当前时间格式化

第一个类:

package totabelaction;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJOptionPane;

import topdfTableToPdf;

import totabelviewTabelData;

import totabelxlsExcelDemo;

public class TableAction implements ActionListener {

TabelData data;

public TableAction(TabelData data) {

thisdata = data;

}

public void actionPerformed(ActionEvent e) {

String str = egetActionCommand();

if ("添加"equals(str)) {

dataaddData();

} else if ("导出到Excel"equals(str)) {

ExcelDemo demo = new ExcelDemo();

demomethod(data);

} else if ("删除"equals(str)) {

if (datagetRow() != -1) {

datadelRow();

} else {

JOptionPaneshowMessageDialog(null, "请选择要删除的行!");

}

}else if("从Excel导入"equals(str)){

datagetXlsInfo();

}else if("从Excel导入到数据库"equals(str)){

datatoDb();

}else if("从table导出到pdf"equals(str)){

TableToPdf pdf=new TableToPdf();

pdfnewPage(data);

}else if("计算学分"equals(str)){

datagetXlsInfoToCredit();

}

}

}

第二个类:数据库连接

package totabeldb;

import javasqlConnection;

import javasqlDriverManager;

import javasqlResultSet;

import javasqlSQLException;

import javasqlStatement;

public class JdbcConnection {

private static JdbcConnection con;

public static JdbcConnection getCon() {

if (con == null) {

con = new JdbcConnection();

}

return con;

}

public Connection getConnection() {

Connection connection=null;

try {

ClassforName("oraclejdbcOracleDriver");

String url = "jdbc:oracle:thin:@127001:1521:oracle";

String user = "scott";

String password = "tiger";

connection = DriverManagergetConnection(url, user,

password);

} catch (ClassNotFoundException e) {

eprintStackTrace();

} catch (SQLException e) {

eprintStackTrace();

}

return connection;

}

// public static void main(String[] args) {

// JdbcConnection connection=new JdbcConnection();

// connectiongetConnection("asd", "99");

// }

}

第三个类:主类(入口)

package totabeldb;

import javasqlConnection;

import javasqlDriverManager;

import javasqlResultSet;

import javasqlSQLException;

import javasqlStatement;

public class JdbcConnection {

private static JdbcConnection con;

public static JdbcConnection getCon() {

if (con == null) {

con = new JdbcConnection();

}

return con;

}

public Connection getConnection() {

Connection connection=null;

try {

ClassforName("oraclejdbcOracleDriver");

String url = "jdbc:oracle:thin:@127001:1521:oracle";

String user = "scott";

String password = "tiger";

connection = DriverManagergetConnection(url, user,

password);

} catch (ClassNotFoundException e) {

eprintStackTrace();

} catch (SQLException e) {

eprintStackTrace();

}

return connection;

}

// public static void main(String[] args) {

// JdbcConnection connection=new JdbcConnection();

// connectiongetConnection("asd", "99");

// }

}

第四个类:

package totabelxls;

import javaioFile;

import javaioIOException;

import javautilVector;

import javautilregexMatcher;

import javautilregexPattern;

import javaxswingJOptionPane;

import totabelviewTabelData;

import jxlCell;

import jxlSheet;

import jxlWorkbook;

import jxlreadbiffBiffException;

import jxlwriteLabel;

import jxlwriteWritableSheet;

import jxlwriteWritableWorkbook;

import jxlwriteWriteException;

import jxlwritebiffRowsExceededException;

public class ExcelDemo {

/

@param args

/

private Vector title = new Vector();

private Vector[] array;

// public static void main(String[] args) {

// ExcelDemo demo = new ExcelDemo();

// demogetXlsInfo();

//

// }

public void method(TabelData table) {

int row = tablegetRowSize();

int column = tablegetColumnSize();

WritableWorkbook book = null;

Vector title = tablesetTitle();

Object[] str = titletoArray();

try {

book = WorkbookcreateWorkbook(new File("testxls"));

WritableSheet sheet = bookcreateSheet("成绩表", 0);

for (int i = 0; i < strlength; i++) {

sheetaddCell(new Label(i, 0, (String) str[i]));

}

for (int i = 1; i < row + 1; i++) {

for (int j = 1; j < column + 1; j++) {

sheetaddCell(new Label(j - 1, i, tablegetTableInfo(i - 1,

j - 1)));

}

}

bookwrite();

JOptionPaneshowMessageDialog(null, "导出完成!");

} catch (IOException e) {

eprintStackTrace();

} catch (RowsExceededException e) {

eprintStackTrace();

} catch (WriteException e) {

eprintStackTrace();

} finally {

try {

bookclose();

} catch (WriteException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

}

/

输出Excel的数据到表单

@return

/

public Vector getXlsInfo() {

Vector v = new Vector();

jxlWorkbook rwb = null;

int index = 0;

try {

rwb = jxlWorkbookgetWorkbook(new File("testxls"));

Sheet[] sheet = rwbgetSheets();

for (int i = 0; i < sheetlength; i++) {

int rs = sheet[i]getRows();

array = new Vector[rs - 1];

for (int j = 1; j < rs; j++) {

Cell[] cell = sheet[i]getRow(j);

Vector info = new Vector();

for (int k = 0; k < celllength; k++) {

infoadd(cell[k]getContents());

}

array[index] = info;

index++;

vadd(info);

}

Cell[] titleCell = sheet[i]getRow(0);

for (int j = 0; j < titleCelllength; j++) {

titleadd(titleCell[j]getContents());

}

}

} catch (BiffException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

} finally {

rwbclose();

}

return v;

}

public Vector getXlsInfoToCredit() {

Vector v = new Vector();

jxlWorkbook rwb = null;

try {

rwb = jxlWorkbookgetWorkbook(new File("d:/test/信科0821(南迁)xls"));

Sheet[] sheet = rwbgetSheets();

for (int i = 0; i < sheetlength; i++) {

int rs = sheet[i]getRows();

array = new Vector[rs - 1];

for (int j = 1; j < rs; j++) {

Cell[] cell = sheet[i]getRow(j);

Vector info = new Vector();

for (int k = 0; k < celllength; k++) {

// if(){

Pattern p = Patterncompile("[0-9]{1,}");

Matcher m = pmatcher(cell[k]getContents());

if (mmatches()) {

int score = IntegervalueOf(cell[k]getContents());

float result = getScore(score);

infoadd(result);

} else {

infoadd(cell[k]getContents());

}

}

vadd(info);

}

Cell[] titleCell = sheet[i]getRow(0);

for (int j = 0; j < titleCelllength; j++) {

titleadd(titleCell[j]getContents());

}

}

} catch (BiffException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

} finally {

rwbclose();

}

return v;

}

public float getScore(int n) {

float score = n;

if (n < 60) {

score = 0;

return score;

} else {

if (n >= 60 && n <= 63) {

score = (float) 10;

} else if (n >= 64 && n <= 67) {

score = (float) 13;

} else if (n >= 68 && n <= 71) {

score = (float) 17;

} else if (n >= 72 && n <= 75) {

score = (float) 20;

} else if (n >= 76 && n <= 79) {

score = (float) 23;

} else if (n >= 80 && n <= 83) {

score = (float) 27;

} else if (n >= 84 && n <= 87) {

score = (float) 30;

} else if (n >= 88 && n <= 91) {

score = (float) 33;

} else if (n >= 92 && n <= 95) {

score = (float) 37;

} else if (n >= 96 && n <= 100) {

score = (float) 40;

}

return score;

}

}

public Vector getTitle() {

// getXlsInfo();

return title;

}

public Vector[] getArray() {

getXlsInfo();

return array;

}

}

因为时间问题就没有再写了,上面是我以前做的,不懂就q我

如果不用ORM框架,那么直接引入ojdbc6jar 驱动包,然后在代码中JDBC配置;

然后可以使用jdbc实现增删改查,比如下面代码

 ResultSet rs = null;  

  Statement stmt = null;  

  Connection conn = null;  

  try {  

   ClassforName("oraclejdbcdriveroracleDriver");  

   //new oraclejdbcdriveroracleDriver();  

   conn = DriverManagergetConnection("jdbc:oracle:thin:@19216801:1521:yuewei", "scott", "tiger");  

   stmt = conncreateStatement();  

   rs = stmtexecuteQuery("select  from dept");  

   while(rsnext()) {  

    Systemoutprintln(rsgetString("deptno"));  

    //Systemoutprintln(rsgetInt("deptno"));  

   }  

  } catch (ClassNotFoundException e) {  

   eprintStackTrace();  

  } catch (SQLException e) {  

   eprintStackTrace();  

  } finally {  

   try {  

    if(rs != null) {  

     rsclose();  

     rs = null;  

    }  

    if(stmt != null) {  

     stmtclose();  

     stmt = null;  

    }  

    if(conn != null) {  

     connclose();  

     conn = null;  

    }  

   } catch (SQLException e) {  

    eprintStackTrace();  

   }  

  }

2

以上就是关于如何使用java对oracle数据库进行增删查改全部的内容,包括:如何使用java对oracle数据库进行增删查改、java代码像oracle数据库中添加数据,代码出错,填不进去、oracle数据库 怎么用java代码把从一张表中查询出来的日期时间插入另一张表中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9734171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存