java从mysql导出数据例子
Jun 07, 2016 pm 03:14 PM欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 * 导出一小时内的数据 * @param conn */ public static void Exp(Connection conn) { int counter = 0; //一小时内的数据 Long timestamp = System.currentTimeMillis() - (600 * 60 * 1000);
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
* 导出一小时内的数据
* @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
{
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();
}
}
}
[1] [2] [3]

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Générateur de nombres aléatoires en Java

Comment corriger les erreurs mysql_native_password non chargé sur MySQL 8.4

Questions d'entretien chez Java Spring
