
java项目用的最普遍的数据库就是mysql和oralce,大多数公司的项目都有用这两者之一,或者两个都用。
Oracle是商业数据库,提供更好的数据服务,主要用于大型的数据库应用。Oracle自身有强大的实力所以他为客户开发了很多数据挖掘检索备份恢复的强大工具,而能用到他的服务的都是拥有海量数据要处理的。比如中国移动的业务数据,每天能产生好几亿的数据,都是用oracle的数据库来存储的。
mysql是开源的产品,免费,所有源码可以根据自身需求修改(只要你有那个能力),主要用于各种中小型的数据应用。如果项目需要数据库服务但是又没有达到海量数据的规模,建议用mysql,因为它是免费得。
总的来说,从性能上来说,oralce数据库优于mysql。
1)从使用上来说,oci必须在客户机上安装oracle客户端或才能连接,而thin就不需要,因此从使用上来讲thin还是更加方便,这也是thin比较常见的原因。
2)原理上来看,thin是纯java实现tcp/ip的c/s通讯;而oci方式,客户端通过native java method调用c library访问服务端,而这个c library就是oci(oracle called interface),因此这个oci总是需要随着oracle客户端安装(从oracle1010开始,单独提供OCI Instant Client,不用再完整的安装client)
3)它们分别是不同的驱动类别,oci是二类驱动, thin是四类驱动,但它们在功能上并无差异。
主类,界面:
package totabelview;
import javaawtBorderLayout;
import javaawtFlowLayout;
import javasqlConnection;
import javasqlSQLException;
import javasqlStatement;
import javautilVector;
import javaxnamingNameParser;
import javaxswing;
import javaxswingtableDefaultTableModel;
import javaxswingtableTableColumnModel;
import totabelactionTableAction;
import totabeldbJdbcConnection;
import totabelxlsExcelDemo;
public class TabelData {
/
用于将table表单中的数据导入到Excel文件中
@param args
/
private int rowSize;
private int columnSize;
private int row;
private JPanel buttonPanel;
private JTable tabel;
private JScrollPane panel;
private DefaultTableModel model;
private JPanel infoPanel;
TableAction action = new TableAction(this);
JTextField name = new JTextField(10);
JTextField score = new JTextField(10);
public static void main(String[] args) {
TabelData data = new TabelData();
datacreateFrame();
}
public void createFrame() {
JFrame frame = new JFrame("Table");
framesetSize(500, 500);
frameadd(createButtonPanel(),BorderLayoutNORTH);
frameadd(createInfoPanel(),BorderLayoutSOUTH);
frameadd(createScrPane(),BorderLayoutCENTER);
framesetLocationRelativeTo(null);
framesetVisible(true);
framesetDefaultCloseOperation(frameEXIT_ON_CLOSE);
}
private JPanel createButtonPanel(){
if(buttonPanel==null){
buttonPanel=new JPanel();
buttonPaneladd(createButton("删除"));
buttonPaneladd(createButton("添加"));
buttonPaneladd(createButton("导出到Excel"));
buttonPaneladd(createButton("从Excel导入"));
buttonPaneladd(createButton("从Excel导入到数据库"));
buttonPaneladd(createButton("从table导出到pdf"));
buttonPaneladd(createButton("计算学分"));//此处为商学院专用学分计算用
}
return buttonPanel;
}
private JPanel createInfoPanel() {
if (infoPanel == null) {
infoPanel = new JPanel();
JLabel nameLabel = new JLabel("姓名");
JLabel scoreLabel = new JLabel("成绩");
infoPaneladd(nameLabel);
infoPaneladd(name);
infoPaneladd(scoreLabel);
infoPaneladd(score);
}
return infoPanel;
}
private JScrollPane createScrPane() {
if (panel == null) {
panel = new JScrollPane(createTable());
}
return panel;
}
public JTable createTable() {
if (tabel == null) {
model = new DefaultTableModel(setTitle(), 0);
tabel = new JTable(model);
tabelsetAutoResizeMode(JTableAUTO_RESIZE_OFF);
}
return tabel;
}
private JButton createButton(String name) {
JButton button = new JButton(name);
buttonaddActionListener(action);
return button;
}
public Vector setTitle() {
Vector title = new Vector();
titleadd("姓名");
titleadd("成绩");
return title;
}
public void addData() {
Vector data1 = new Vector();
data1add(namegetText()trim());
data1add(scoregetText()trim());
modeladdRow(data1);
}
/
得到行数和列数
@return
/
public int getColumnSize() {
columnSize = tabelgetColumnCount();
return columnSize;
}
public int getRowSize() {
rowSize = tabelgetRowCount();
return rowSize;
}
/
按行列得到信息
@param row
@param column
@return
/
public String getTableInfo(int row, int column) {
String info = (String) tabelgetValueAt(row, column);
return info;
}
public void delRow() {
while (row != -1) {
getRow();
modelremoveRow(row);
getRow();
}
}
public int getRow() {
row = tabelgetSelectedRow();
return row;
}
/
将Excel数据导入到表单
/
public void getXlsInfo() {
ExcelDemo demo = new ExcelDemo();
modelsetDataVector(demogetXlsInfo(), demogetTitle());
}
public void getXlsInfoToCredit() {
ExcelDemo demo = new ExcelDemo();
modelsetDataVector(demogetXlsInfoToCredit(), demogetTitle());
}
public void toDb() {
ExcelDemo demo = new ExcelDemo();
JdbcConnection connection = JdbcConnectiongetCon();
Connection con = connectiongetConnection();
Statement sta = null;
try {
sta = concreateStatement();
} catch (SQLException e1) {
e1printStackTrace();
}
Vector[] v = demogetArray();
for (int i = 0; i < vlength; i++) {
String name = v[i]get(0)toString();
String score = v[i]get(1)toString();
Systemoutprintln(name + " " + score);
try {
String sql = "insert into score(stu_name,stu_score) values ('" + name
+ "'," + score + ")";
staexecuteUpdate(sql);
} catch (SQLException e) {
eprintStackTrace();
// Systemoutprintln("erro");
}
}
}
}
第二个类:
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 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;
}
}
我的是导出到xls中,基本原理是一样的,希望对楼主有用把
以上就是关于java请问一般都用什么数据库连接啊全部的内容,包括:java请问一般都用什么数据库连接啊、Oracle连接 Oracle 的OCI Driver 和 Thin Driver的区别、java 程序 怎样导入导出informix的表数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)