日期:2014-05-20  浏览次数:20712 次

java导入数据到mysql
大神精简下·······

import java.io.*;
import java.sql.*;
import java.sql.Date;
import java.text.*;

import java.util.*;

import javax.xml.crypto.Data;

import sun.management.FileSystem;
public class student{
  public void intiParam()throws Exception
  {
File f=new File("E:/myschool","student.txt");

BufferedReader buffin = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
String temp;
String b[]=new String[100];
int i=0;
byte c[]=new byte[9000];
String option[]=new String[100];
while((temp=buffin.readLine())!=null)
{  
c=temp.getBytes();
option[i]=new String(c);
System.out.println(option[i])
;
i++;
}
for (int j = 0; j < option.length; j++) {

if(option[j]==null)
break;
 
StringTokenizer stz=new StringTokenizer(option[j], ",");
int k=0;
while(stz.hasMoreTokens()){
b[k]=stz.nextToken();
System.out.println(b[k]);
k++;
if(k%9==0){

String studentno=b[k-9];
String loginpwd=b[k-8];
String studentname=b[k-7];
char sex[]=b[k-6].toCharArray(); 
int gradeid=Integer.parseInt(b[k-5]);
String phone=b[k-4];
String address=b[k-3];
String bormdate=b[k-2];
String email=b[k-1];
  int d=update(studentno, loginpwd, studentname, sex[0], gradeid, phone, address, bormdate, email);
  System.out.println(d);
System.out.println();
System.out.println();
System.out.println();
}
}
}
 
}
  
  private int update(String studentno,String loginpwd,String studentname,char sex,int gradeid ,String phone,String address,String bormdate,String email)
  { int b=0;
  try {
  Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root", "123456");
Statement st=con.createStatement();
// Date date=null;
Timestamp date=null;
try {
// date=java.sql.Date.valueOf(bormdate);
date= Timestamp.valueOf(bormdate);

} catch (Exception e) {
// TODO: handle exception
}
String sql="insert into student values('"+studentno+"','"+loginpwd+"','"+studentname+"','"+sex+"',"+gradeid+",'"+phone+"','"+address+"','"+date+"','"+email+"');";
b=st.executeUpdate(sql);
return b;
} catch (Exception e) {
// TODO: handle exception
}
 
  return b;
  }
  public static void main(String[] args) {
student d=new student();
try {
d.intiParam();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


------解决方案--------------------
有什么问题吗