问题描述
我是android开发的新手,老板给了我一个android项目作为我的任务。我尝试使用JTDS以编程方式从Android设备将csv文件导入SQL Server。 不幸的是,代码只运行到以下这一行: BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
这是我的密码。没有出现错误,但csv文件仍然无法加载到SQL Server中。
public class ImportCSV extends AsyncTask<String,String,String> {
String z = "";
protected void onPreExecute() {
//pbbar.setVisibility(View.VISIBLE);
}
protected void onPostExecute(String r) {
z = "Success";
}
protected String doInBackground(String... params) {
connectionClass = new ConnectionClass();
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String csvFilePath = "PumpRecord.csv";
int batchSize = 20;
try {
String sql = "INSERT INTO pump (APA_Sticker_No,Pump1,Pump2,Cut_out,Cut_in,Remark,ByUser,Location,EQP_Type,Transaction_Date ) VALUES (?,?,?)";
PreparedStatement statement = con.prepareStatement(sql);
BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
String lineText = null;
int count = 0;
lineReader.readLine(); // skip header line
while ((lineText = lineReader.readLine()) != null) {
String[] data = lineText.split(",");
String APA_Sticker_No = data[0];
String Pump1 = data[1];
String Pump2 = data[2];
String Cut_out = data[3];
String Cut_in = data[4];
String Remark = data[5];
String ByUser = data[6];
String Location = data[7];
String EQP_Type = data[8];
String Transaction_Date = data[9];
statement.setString(1,APA_Sticker_No);
statement.setString(2,Pump1);
statement.setString(3,Pump2);
statement.setString(4,Cut_out);
statement.setString(5,Cut_in);
statement.setString(6,Remark);
statement.setString(7,ByUser);
statement.setString(8,Location);
statement.setString(9,EQP_Type);
statement.setString(10,Transaction_Date);
statement.addBatch();
if (count % batchSize == 0) {
statement.executeBatch();
z = "Success";
}
}
lineReader.close();
// execute the remaining queries
statement.executeBatch();
z = "success";
con.commit();
con.close();
} catch (IOException ex) {
System.err.println(ex);
} catch (SQLException ex) {
ex.printStackTrace();
try {
con.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return z;
}
}
这是我的连接类:
package com.magicrf.uhfreader.view;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionClass {
String ip = "10.115.2.28"; //server
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "testing";
String un = "tiqa";
String password = "tiqa123";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";integratedSecurity=true;"
+ "databaseName=" + db + ";" +"user=" + un + ";password="+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO",se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO",e.getMessage());
} catch (Exception e) {
Log.e("ERRO",e.getMessage());
}
return conn;
}
}
请帮助我解决此问题。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)