package com.test.demo.test;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Sdemo {
//创建数据库连接
static Connectionconn =null;
//创建预编译语句对象
static Statementpstmt =null;
//创建一个结果集
static ResultSetresult =null;
private static ArrayListtest =new ArrayList();
/**
* 初始化数据库连接驱动
*/
private static void initJdbc() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
String url ="jdbc:mysql://localhost:3306/mock?useUnicode=true&characterEncoding=utf-8"; //数据库连接信息
String user ="root"; //数据库用户名
String pass ="666666"; //数据库密码
conn = DriverManager.getConnection(url, user, pass); //获取连接
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
private static void readFile()throws IOException {
/* 读入TXT文件 */
String path ="/Users/jinhongmei/1/tablename.txt";// 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
File filename =new File(path); // 要读取以上路径的input。txt文件
Scanner myReader =new Scanner(filename);
while (myReader.hasNextLine()) {
String data =myReader.nextLine();
test.add(data);
}
System.out.println(test);
myReader.close();
}
//插入数据
public static void insertIntoTable()throws SQLException {
initJdbc();
int count =0;
for(String tablename:test){
String sql ="select count(1) from " + tablename;
pstmt =conn.prepareStatement(sql);
result=pstmt.executeQuery(sql);
// System.out.println(sql);
if(result.next()) {
count=result.getInt(1);
System.out.println(tablename+","+count );
}
}
}
public static void main(String[] args)throws IOException, SQLException {
readFile();
insertIntoTable();
}
}
网友评论