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

100分看个错误
public   class   sqllink
{    
public   static   void     read(String[]   strStr,int   countLine)   throws   FileNotFoundException
{
try{File   file   =   new   File( "WINPY.TXT ");
FileReader   reader   =   new   FileReader(file);
BufferedReader   br   =   new   BufferedReader(reader);
//String   str1=null;
//countLine=0;
//String   []   str;
while   ((br.readLine())!=null)
{++countLine;
strStr=br.readLine().split( "\t ");
}
br.close();
reader.close();}
catch(IOException   e){System.out.println(e.getMessage());};
}
public     static   void   main   (String[]   args )   throws   ClassNotFoundException,   SQLException,   InstantiationException,   Exception
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String   str   = "jdbc:microsoft:sqlserver://192.168.50.60:1433;DatabaseName=word ";
String   user   =   "sa ";
String   pass   =   "software ";
Connection   conn   =   DriverManager.getConnection(str,user,pass);
Statement   stmt   =   conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String[]   strStr   =   null;int   countLine=0;
read(strStr,countLine);
for(int   i=0;i <=countLine;i++){
String   sql   =   "insert   into   worditem   set(word,worditem)   values( ' "+strStr[i]+ " ', ' "+strStr[i+1]+ " ') ";
int   rs=stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
}
}

我的程序     提示错误为下
Exception   in   thread   "main "   java.lang.NullPointerException
at   wangsong.data.sqllink.read(sqllink.java:31)
at   wangsong.data.sqllink.main(sqllink.java:46)


------解决方案--------------------
read函数什么也没做
并且
while ((br.readLine())!=null)
{++countLine;
strStr=br.readLine().split( "\t ");
}
肯定要抛异常,读到最后就是null.split( "\t ");
------解决方案--------------------
String temp = null;
while ((temp = br.readLine())!=null)
{++countLine;
strStr=temp.split( "\t ");
}
------解决方案--------------------
用数组不管1维或者2维,你都得知道文件行数,伤脑筋。呵呵
如果是我,我就不用数组用Vector:
先遍历文件,把每一行存到1个Vector
然后遍历Vector
{
对其中每一个元素(文件中1行)分解;(当然要注意有空行,1行1个\t ,或者空数据的问题)
组织SQL存数据库
}
这样的代码比较清晰明,但这样的代码效率不好

或者干脆Vector也不用,你读一行就存一次,这样你都不用耗费内存去缓存整个文件的数据:
遍历文件(读一行){
如果此行为空,continie;//空行,属于坏数据
分解此行成为1个数组
判断数组为空或者数组中的数据有为空continue;//坏数据(当然你要考虑数组中如果有NULL的情况,这个貌似业务问题,这里很关键,多用些log吧)
组织SQL存数据库
}
这样的代码可能稍难阅读,不过内存占用会低些,遍历次数也少些。

------解决方案--------------------
WINPY.TXT有问题