「javasql转存」java导出sql脚本
今天给各位分享javasql转存的知识,其中也会对java导出sql脚本进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java.util.Date和java.sql.Date的区别和相互转化
- 2、java代码怎样将oracle数据库中数据下载本地,为.txt文件或者.excel文件。
- 3、Java调用SQL存储过程、事务
- 4、如何将String类型的日期转换成java.sql.Date类型存进数据库
java.util.Date和java.sql.Date的区别和相互转化
区别:
Java.util.Date 是 java.sql.Date 的父类(注意拼写) 前者是常用的表示时间的类,通常格式化或者得到当前时间都是用
后者之后在读写数据库的时候用,因为PreparedStament的setDate()的第2参数和ResultSet的 getDate()方法的第2个参数都是java.sql.Date
转换是 java.sql.Date date=new Java.sql.Date(); java.util.Date d=new java.util.Date (date.getTime()); 反过来是一样的。
java.util.Date实际是一个时间戳,数值是年月日包括当前的时间 java.sql.Date也是一个时间戳,但把当前的时间剪掉了,保证一定是那个日期的0点0分0秒的时间戳
所以如果用new java.util.Date()会得到当前时间 但如果用new java.sql.Date(new java.util.Date().getTime()),得到的时间会是那一天的0点0分0秒
格式如下:
java.util.Date 是 年-月-日 时:分:秒.毫秒
java.sql.Date 是 年-月-日 java.sql.Time 是 时:分:秒
java.sql.Timestamp 是 年-月-日 时:分:秒
1、将java.util.Date 转换为 java.sql.Date
java.sql.Date sd;
java.util.Date ud;
//initialize the ud such as ud = new java.util.Date();
sd = new java.sql.Date(ud.getTime());
2、若要插入到数据库并且相应的字段为Date类型 可使用PreparedStatement.setDate(int ,java.sql.Date)方法 其中的java.sql.Date可以用上面的方法得到
也可以用数据库提供TO_DATE函数 比如 现有 ud TO_DATE(new SimpleDateFormat().format(ud,"yyyy-MM-dd HH:mm:ss"), "YYYY-MM-DD HH24:MI:SS")
注意java中表示格式和数据库提供的格式的不同 sql="update tablename set timer=to_date('"+x+"','yyyymmddhh24miss') where ....."
这里的x为变量为类似:20080522131223
3、如何将"yyyy-mm-dd"格式的字符串转换为java.sql.Date
方法1 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateStringToParse = "2007-7-12";
try{ java.util.Date date = bartDateFormat.parse(dateStringToParse);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(sqlDate.getTime()); }
catch (Exception ex) {
System.out.println(ex.getMessage()); }
方法2
String strDate = "2002-08-09";
StringTokenizer st = new StringTokenizer(strDate, "-");
java.sql.Date date = new java.sql.Date(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
java.util.Date和java.sql.Date的区别
java.sql.Date,java.sql.Time和java.sql.Timestamp三个都是java.util.Date的子类(包装类)。
java.sql.Date是为了配合SQL DATE而设置的数据类型。“规范化”的java.sql.Date只包含年月日信息,时分秒毫秒都会清零。格式类似:YYYY-MM-DD。
当调用ResultSet的getDate()方法来获得返回值时,java程序会参照"规范"的java.sql.Date来格式化数据库中的数值。因此,如果数据库中存在的非规范化部分的信息将会被劫取。
在sun提供的ResultSet.java中这样对getDate进行注释的: Retrieves the of the designated column in the current row of this codeResultSet/code object as a “java.sql.Date” object in the Java programming language.
同理。如果把一个java.sql.Date值通过PrepareStatement的setDate方法存入数据库时,java程序会对传入的java.sql.Date规范化,非规范化的部分将会被劫取。
然而,java.sql.Date一般由java.util.Date转换过来,如:java.sql.Date sqlDate=new java.sql.Date(new java.util.Date().getTime()).
显然,这样转换过来的java.sql.Date往往不是一个规范的java.sql.Date.要保存java.util.Date的精确值, 需要利用java.sql.Timestamp. Calendar:
Calendar calendar=Calendar.getInstance(); //获得当前时间,声明时间变量
int year=calendar.get(Calendar.YEAR); //得到年
int month=calendar.get(Calendar.MONTH); //得到月,但是,月份要加上1
month=month+1;
int date=calendar.get(Calendar.DATE);
//获得日期 String today=""+year+"-"+month+"-"+date+"";
java代码怎样将oracle数据库中数据下载本地,为.txt文件或者.excel文件。
第一个类:
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 = 0; i str.length; i++) {
sheet.addCell(new Label(i, 0, (String) str[i]));
}
for (int i = 1; i row + 1; i++) {
for (int j = 1; j column + 1; j++) {
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 = 0; i sheet.length; 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 cell.length; k++) {
info.add(cell[k].getContents());
}
array[index] = info;
index++;
v.add(info);
}
Cell[] titleCell = sheet[i].getRow(0);
for (int j = 0; j titleCell.length; j++) {
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 = 0; i sheet.length; 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 cell.length; k++) {
// 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 = 0; j titleCell.length; j++) {
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调用SQL存储过程、事务
例示一个不带参数的简单存储过程。多数存储过程的功能比本例复杂多了,但这里主要说明存储过程的一些基本要点。如前面所述,不同DBMS定义存储过程的语法是不同的。例如,有些DBMS使用begin
.
.
.
end或其他关键字指明存储过程定义的开始和结束。在有些DBMS中,下面的SQL语句可创建一个存储过程:
create
procedure
SHOW_SUPPLIERS
as
select
SUPPLIERS.SUP_NAME,
COFFEES.COF_NAME
from
SUPPLIERS,
COFFEES
where
SUPPLIERS.SUP_ID
=
COFFEES.SUP_ID
order
by
SUP_NAME
下面的代码将SQL语句放到一个字符串中,然后赋给变量createProcedure以备后用:
String
createProcedure
=
"create
procedure
SHOW_SUPPLIERS
"
+
"as
"
+
"select
SUPPLIERS.SUP_NAME,
COFFEES.COF_NAME
"
+
"from
SUPPLIERS,
COFFEES
"
+
"where
SUPPLIERS.SUP_ID
=
COFFEES.SUP_ID
"
+
"order
by
SUP_NAME";
下面的代码段使用Connection对象con来创建Statement对象,用于把创建存储过程的SQL语句发送给数据库:
Statement
stmt
=
con.createStatement();
stmt.executeUpdate(createProcedure);
存储过程SHOW_SUPPLIERS将作为一个可调用的数据库对象在数据库中编译并存储,调用时就像调用其他方法一样。
如何将String类型的日期转换成java.sql.Date类型存进数据库
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(String的date);
转化的就是date了
javasql转存的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java导出sql脚本、javasql转存的信息别忘了在本站进行查找喔。