java代码怎样将oracle数据库中数据下载本地,为.txt文件或者.excel文件。

java代码怎样将oracle数据库中数据下载本地,为.txt文件或者.excel文件。,第1张

第一个类:

package totabel.action

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JOptionPane

import topdf.TableToPdf

import totabel.view.TabelData

import totabel.xls.ExcelDemo

public class TableAction implements ActionListener {

TabelData data

public TableAction(TabelData data) {

this.data = data

}

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand()

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

data.addData()

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

ExcelDemo demo = new ExcelDemo()

demo.method(data)

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

if (data.getRow() != -1) {

data.delRow()

} else {

JOptionPane.showMessageDialog(null, "请选择要删除的行!")

}

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

data.getXlsInfo()

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

data.toDb()

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

TableToPdf pdf=new TableToPdf()

pdf.newPage(data)

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

data.getXlsInfoToCredit()

}

}

}

第二个类:数据库连接

package totabel.db

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.SQLException

import java.sql.Statement

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 {

Class.forName("oracle.jdbc.OracleDriver")

String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle"

String user = "scott"

String password = "tiger"

connection = DriverManager.getConnection(url, user,

password)

} catch (ClassNotFoundException e) {

e.printStackTrace()

} catch (SQLException e) {

e.printStackTrace()

}

return connection

}

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

// JdbcConnection connection=new JdbcConnection()

// connection.getConnection("asd", "99")

// }

}

第三个类:主类(入口)

package totabel.db

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.SQLException

import java.sql.Statement

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 {

Class.forName("oracle.jdbc.OracleDriver")

String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle"

String user = "scott"

String password = "tiger"

connection = DriverManager.getConnection(url, user,

password)

} catch (ClassNotFoundException e) {

e.printStackTrace()

} catch (SQLException e) {

e.printStackTrace()

}

return connection

}

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

// JdbcConnection connection=new JdbcConnection()

// connection.getConnection("asd", "99")

// }

}

第四个类:

package totabel.xls

import java.io.File

import java.io.IOException

import java.util.Vector

import java.util.regex.Matcher

import java.util.regex.Pattern

import javax.swing.JOptionPane

import totabel.view.TabelData

import jxl.Cell

import jxl.Sheet

import jxl.Workbook

import jxl.read.biff.BiffException

import jxl.write.Label

import jxl.write.WritableSheet

import jxl.write.WritableWorkbook

import jxl.write.WriteException

import jxl.write.biff.RowsExceededException

public class ExcelDemo {

/**

*

* @param args

*/

private Vector title = new Vector()

private Vector[] array

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

// ExcelDemo demo = new ExcelDemo()

// demo.getXlsInfo()

//

// }

public void method(TabelData table) {

int row = table.getRowSize()

int column = table.getColumnSize()

WritableWorkbook book = null

Vector title = table.setTitle()

Object[] str = title.toArray()

try {

book = Workbook.createWorkbook(new File("test.xls"))

WritableSheet sheet = book.createSheet("成绩表", 0)

for (int i = 0i <str.lengthi++) {

sheet.addCell(new Label(i, 0, (String) str[i]))

}

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

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

sheet.addCell(new Label(j - 1, i, table.getTableInfo(i - 1,

j - 1)))

}

}

book.write()

JOptionPane.showMessageDialog(null, "导出完成!")

} catch (IOException e) {

e.printStackTrace()

} catch (RowsExceededException e) {

e.printStackTrace()

} catch (WriteException e) {

e.printStackTrace()

} finally {

try {

book.close()

} catch (WriteException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}

}

}

/**

* 输出Excel的数据到表单

*

* @return

*/

public Vector getXlsInfo() {

Vector v = new Vector()

jxl.Workbook rwb = null

int index = 0

try {

rwb = jxl.Workbook.getWorkbook(new File("test.xls"))

Sheet[] sheet = rwb.getSheets()

for (int i = 0i <sheet.lengthi++) {

int rs = sheet[i].getRows()

array = new Vector[rs - 1]

for (int j = 1j <rsj++) {

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

Vector info = new Vector()

for (int k = 0k <cell.lengthk++) {

info.add(cell[k].getContents())

}

array[index] = info

index++

v.add(info)

}

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

for (int j = 0j <titleCell.lengthj++) {

title.add(titleCell[j].getContents())

}

}

} catch (BiffException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

rwb.close()

}

return v

}

public Vector getXlsInfoToCredit() {

Vector v = new Vector()

jxl.Workbook rwb = null

try {

rwb = jxl.Workbook.getWorkbook(new File("d:/test/信科0821(南迁).xls"))

Sheet[] sheet = rwb.getSheets()

for (int i = 0i <sheet.lengthi++) {

int rs = sheet[i].getRows()

array = new Vector[rs - 1]

for (int j = 1j <rsj++) {

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

Vector info = new Vector()

for (int k = 0k <cell.lengthk++) {

// if(){

Pattern p = Pattern.compile("[0-9]{1,}")

Matcher m = p.matcher(cell[k].getContents())

if (m.matches()) {

int score = Integer.valueOf(cell[k].getContents())

float result = getScore(score)

info.add(result)

} else {

info.add(cell[k].getContents())

}

}

v.add(info)

}

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

for (int j = 0j <titleCell.lengthj++) {

title.add(titleCell[j].getContents())

}

}

} catch (BiffException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

rwb.close()

}

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) 1.0

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

score = (float) 1.3

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

score = (float) 1.7

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

score = (float) 2.0

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

score = (float) 2.3

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

score = (float) 2.7

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

score = (float) 3.0

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

score = (float) 3.3

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

score = (float) 3.7

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

score = (float) 4.0

}

return score

}

}

public Vector getTitle() {

// getXlsInfo()

return title

}

public Vector[] getArray() {

getXlsInfo()

return array

}

}

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

拷过来的,你看看吧,我就是按这个做的。

Java连接数据库过程:

第一步:驱动安装和路径设置Step1:到mysql的官方网站下载mysql-connector-java-5.1.18.zip驱动,解压以后复制里面的mysql-connector-java-5.1.18.jar到jdk安装目录的lib文件夹中;Step2:设置环境变量CLASSPATH=.%JAVA_HOME%\lib\mysql-connector-java-5.1.18.jar

第二步:程序编写

Step1:在程序中加载并注册JDBC驱动器,其中JDBC-ODBC在JDK自带,默认已经注册,所以不需要注册;

Class.forName(“com.mysql.jdbc.Driver”)

java.sql.DriverManager.registerDriver(newcom.mysql.jdbc.Driver())//对于MySQL不是必须

Step2:建立与数据库的连接

Connection con =java.sql.DriverManager.getConnection(dburl,user,pwd)

dburl表示连接数据库的JDBC URL: jdbc:mysql://localhost:3306/DB_NAME

user: 数据库用户名

pwd:数据库密码

Step3:创建Statement对象,准备执行SQL语句

Statement stmt = con.createStatement()

Step4: 执行SQL语句

String sql = “select * from tb_name”

ResultSet rs = stmt.executeQuery(sql)

Step5:遍历ResultSet对象中的记录

while(rs.next()){

String name = rs.getString(index)

Long id = rs.getLong(index)

Int age = rs.getInt(index)

}

Step6:关闭数据库

rs.close()

stmt.close()

con.close()

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yaoxy/archive/2009/08/06/4418116.aspx

其实*.db只是创建数据库文件之初命名出来的后缀,那么*.db怎么解析呢?

1 假如我们用的是mysql数据库,保存指定好的数据库文件为mysql.db,那么就目前应用所了解,就是通过mysql数据库运行起来后读取加载此文件,然后再通过java程序或者C程序或者其他语言程序连接已启动的数据库,读取此数据库文件;

2 如果*.db是安卓的sqlLite数据库创建出来指定的数据库文件,那么我们还是要借助安卓系统自带数据库系统加载此*.db文件,然后通过java程序连接数据库读取里面内容;

3 如果*.db是derby数据库创建出来的,那么道理类似;

4 但是,假如*.db非数据库文件,仅仅命名为db格式,未知?文本?语音?视频?库文件。。。那么以上那些都不管用了,但是既然有个*.db,那么肯定它的来源我们是清楚的,从何种数据库中产生,用何种数据库去加载,然后程序读取即可。


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

原文地址:https://54852.com/sjk/6821581.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存