我试图转移" data.txt"文件到数据库.txt文件包含数据包
(**S12*T0*0889*B*99*N1C0~**S12*T0*0880*B*99*N1C0~**S12*T0*0889*B*99*N1C0~)...
这里是我根据"〜"来分隔数据的代码:
public static void main(String[] args) throws IOException {
String token1 = "";// create token1 (deceleration only)
List<String> temps = new ArrayList<String>();// Array Deceleration only
Scanner inFile1 = new Scanner(new File("2.txt"));
inFile1.useDelimiter("~ *");
while (inFile1.hasNext()) {// while loop
token1 = inFile1.next();// find next line in inFile1
temps.add(token1);
}
inFile1.close();`
现在我还需要将&#34; *&#34;之间的数据分开。并插入database.Code进行数据库插入:
String[] tempsArray = temps.toArray(new String[0]);
PreparedStatement ps = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Data Source Driver
Connection con=DriverManager.getConnection("jdbc:odbc:testing");//Data Connection
for(String s : tempsArray){
ps=con.prepareStatement("insert into testing (fileName)values('"+s+"')");//inserting data
System.out.println(s);//printing inserted data
System.out.println("inserted");//insertion confirmation
ps.executeUpdate();
}
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}