读文件入库


public static void game_ua() {
Connection conn = null;
PreparedStatement stmt_user = null;
ResultSet rs_user = null;
BufferedReader bufferedreader = null;
String stemp;
try {
bufferedreader = new BufferedReader(new FileReader("c:/test.txt"));
conn = mysql();
String sql_g_add = "insert into T_GAME_UA_SERIES(ua,series_id) values(?,?)";

stmt_user = conn.prepareStatement(sql_g_add);
int i=0;
while ((stemp = bufferedreader.readLine()) != null) {
i++;
System.out.println(i+": "+stemp);
stmt_user.setString(1, stemp);
stmt_user.setInt(2, 1);
//stmt_user.execute();
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs_user != null) {
try {
rs_user.close();
rs_user = null;
} catch (SQLException e) {
rs_user = null;
}
}
if (stmt_user != null) {
try {
stmt_user.close();
stmt_user = null;
} catch (SQLException e) {
e.printStackTrace();
stmt_user = null;
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
conn = null;
}
}
}
}


private static Connection mysql(){
final String userName = "mysql";
final String password = "mysql";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/test";
conn = DriverManager.getConnection(url, userName, password);

} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}

public static ArrayList readToBuffer(StringBuffer buffer, InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
ArrayList all = new ArrayList();
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
all.add(line); // set 到 ArrayList 中
// buffer.append("\\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
return all;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值