当前位置: 技术问答>java相关
如何从数据库中提取数据生成文本文件
来源: 互联网 发布时间:2017-03-26
本文导语: 从MS SQL2000里读出数据,将数据以一定的格式生成文本文件,用java怎么实现呀!给点儿建议。 | import java.sql.*; import java.io.*; public class Sqlshow { public static void main(String args[]){ ...
从MS SQL2000里读出数据,将数据以一定的格式生成文本文件,用java怎么实现呀!给点儿建议。
|
import java.sql.*;
import java.io.*;
public class Sqlshow {
public static void main(String args[]){
Sqlshow d = new Sqlshow();
d.getdate();
}
public void getdate() {
try{
PrintStream pos = new PrintStream(new FileOutputStream("EMP.DAT"));
String ename;
long empid;
double sal;
Date hiredate;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注册DRIVER
Connection conn1 = DriverManager.getConnection("jdbc:odbc:sql_emp","sa","");//建立连接
Statement state= conn1.createStatement();//如连接成功返回STATEMENT对象
//利用STATEMENT可执行普通的SQL语句
ResultSet set = state.executeQuery("select id,name,hiredate,sal from emp");
//先打印表头*/*****************************************
pos.println("id name hiredate sal");
while(set.next()){
empid=set.getLong(1);
ename=set.getString(2);
hiredate=set.getDate(3);
sal = set.getDouble(4);
//打印表内容**************************************
pos.println(empid+" "+ename+" "+hiredate+" "+sal);
}
}catch (SQLException e){
System.out.println(e.getErrorCode());
}
catch (ClassNotFoundException e){
System.out.println("class not found");
}
catch (IOException e){
System.out.println("IOException");
}
}
}
import java.io.*;
public class Sqlshow {
public static void main(String args[]){
Sqlshow d = new Sqlshow();
d.getdate();
}
public void getdate() {
try{
PrintStream pos = new PrintStream(new FileOutputStream("EMP.DAT"));
String ename;
long empid;
double sal;
Date hiredate;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注册DRIVER
Connection conn1 = DriverManager.getConnection("jdbc:odbc:sql_emp","sa","");//建立连接
Statement state= conn1.createStatement();//如连接成功返回STATEMENT对象
//利用STATEMENT可执行普通的SQL语句
ResultSet set = state.executeQuery("select id,name,hiredate,sal from emp");
//先打印表头*/*****************************************
pos.println("id name hiredate sal");
while(set.next()){
empid=set.getLong(1);
ename=set.getString(2);
hiredate=set.getDate(3);
sal = set.getDouble(4);
//打印表内容**************************************
pos.println(empid+" "+ename+" "+hiredate+" "+sal);
}
}catch (SQLException e){
System.out.println(e.getErrorCode());
}
catch (ClassNotFoundException e){
System.out.println("class not found");
}
catch (IOException e){
System.out.println("IOException");
}
}
}
|
系统数据源DSN设置正确了吗
|
up