java从mysql导出数据例子
来源:互联网 发布:数据资源共享合作协议 编辑:程序博客网 时间:2024/06/05 11:40
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
public static void main(String[] args) {
//Test(); // 生成测试数据
//Exp();
Exp(0);
//System.out.println(readText("/opt/id.txt"));
}
/**
* 导出数据
*/
public static void Exp() {
Connection Conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=GBK";
//String jdbcUsername = "root";
//String jdbcPassword = "mysql";
Conn = DriverManager.getConnection(jdbcUrl, "root", "mysql");
System.out.println("conn"+Conn);
Exp(Conn);
} catch (SQLException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void Exp(int startid) {
Connection Conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=GBK";
String jdbcUsername = "root";
String jdbcPassword = "mysql";
Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
System.out.println("conn"+Conn);
Exp(Conn,startid);
} catch (SQLException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 导出从startid开始的数据
* @param conn
* @param start_id
*/
public static void Exp(Connection conn,int start_id) {
int counter = 0;
int startid=start_id;
boolean flag = true;
while (flag) {
flag = false;
String Sql = "SELECT * FROM t_test WHERE id>"
+ startid + " order by id asc LIMIT 50";
System.out.println("sql===" + Sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(Sql);
while (rs.next()) {
flag = true;
int id = rs.getInt("id");
String title = rs.getString("title");
startid = id ;
counter++;
writeContent(counter+"--id--"+id+"--title-"+title+"\r\n", "D:\\","log.txt",true);
System.out.println("i="+counter+"--id--"+id+"--title-"+title);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
writeContent(""+startid, "D:\\","id.txt",false);
}
/**
* 导出一小时内的数据
* @param conn
*/
public static void Exp(Connection conn) {
int counter = 0;
//一小时内的数据
Long timestamp = System.currentTimeMillis() - (600 * 60 * 1000);
boolean flag = true;
while (flag) {
flag = false;
String Sql = "SELECT * FROM t_test WHERE createTime>"
+ timestamp + " LIMIT 50";
System.out.println("sql===" + Sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(Sql);
while (rs.next()) {
flag = true;
int id = rs.getInt("id");
String title = rs.getString("title");
Long lastmodifytime = rs.getLong("createTime");
timestamp = lastmodifytime;
counter++;
System.out.println("i="+counter+"--id--"+id+"--title-"+title);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void Test() {
Connection Conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=GBK";
String jdbcUsername = "root";
String jdbcPassword = "mysql";
Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
System.out.println("conn"+Conn);
for(int i=1;i<=10000;i++)
{
add(Conn,"testTitle"+i+"-"+System.currentTimeMillis());
}
} catch (SQLException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
Conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void add(Connection conn,String title)
{
PreparedStatement pstmt = null;
String insert_sql = "insert into t_test(title,createTime) values (?,?)";
System.out.println("sql="+insert_sql);
try {
pstmt = conn.prepareStatement(insert_sql);
pstmt.setString(1,title);
pstmt.setLong(2,System.currentTimeMillis());
int ret = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 写入内容到文件
*
* @param number
* @param filename
* @return
*/
public static boolean writeContent(String c, String dirname,String filename,boolean isAppend) {
File f=new File(dirname);
if (!f.exists())
{
f.mkdirs();
}
try {
FileOutputStream fos = new FileOutputStream( dirname+File.separator+filename,isAppend);
OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.write(c);
writer.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 从文件读取内容
*
* @param filename
* @return
*/
public static String readText(String filename) {
String content = "";
try {
File file = new File(filename);
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str = "";
String newline = "";
while ((str = br.readLine()) != null) {
content += newline + str;
newline = "\n";
}
br.close();
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
}
- java从mysql导出数据例子
- 从mysql中导出表及数据
- sqoop从hbase导出数据到mysql
- 从MySQL数据库中导出数据
- 从mysql 中批量导出数据
- SQOOP从HDFS导出数据到MySQL
- 使用pandas从mysql中导出数据
- java导入导出mysql数据
- 从mysql导出数据,不导出表结构
- 从JAVA导出数据到Excel
- Java实现从网页表格导出CSV文件的例子
- java 调用mysql客户端导入,导出数据
- java中jxl导出数据到excel的例子
- java中jxl导出数据到excel的例子
- 使用Java 导出大表数据到文件的例子
- java中jxl导出数据到excel的例子
- java中jxl导出数据到excel的例子
- 从 MySQL 导入导出大量数据的程序
- java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
- mysql学习笔记
- IOS真机调试设置
- R12中注册应用为多组织应用
- string 函数原型
- java从mysql导出数据例子
- web.xml报错
- fedora9 换源方法
- html中文部乱码,jsp全部乱码。
- Delphi备忘录——数据类型
- Visual C++ 开发书籍
- 文本数据导入HBASE库找不到类com/google/common/collect/Multimap
- linux 下搭建 ftp
- 关于ie6的a标签的那点事