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)
{
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)
空值 定位下 DEBUG,数据库连上了?
read(strStr,countLine);
while ((br.readLine())!=null)
{++countLine;
strStr=br.readLine().split("\t");
}
// 你的文件行数 等于 数姐的 length 吗?
==================================================
这一句有问题啊,你传进去的只是strStr的一个副本,你的read函数并不会改变main函数中的strStr[]这个数组,所以你去strStr[i]肯定就抛异常了,因为它不像C那样去操作数组,只是地址的一个副本
建议你改成返回数组的形式
public static String[] read(int contline);{String str[]= null;.................return str};
在main 函数里面接一下strStr = read(xx);然后判断if(strStr != null) 再进行操作
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.Date;
import java.util.HashMap
at wangsong.data.sqllink.read(sqllink.java:31)
at wangsong.data.sqllink.main(sqllink.java:46)
象这类问题,我觉得楼主还是自己DEBUG解决比较好,目标很明确呀,只要DEBUG就知道了呀,以后类似的问题,也这样呀,这对你自己解决问题的能力提高也有帮助呀。
并且
while ((br.readLine())!=null)
{++countLine;
strStr=br.readLine().split("\t");
}
肯定要抛异常,读到最后就是null.split("\t");
{++countLine;
strStr=br.readLine().split("\t");
}
肯定要抛异常,读到最后就是null.split("\t");
================================================
我晕
没看到while条件?
空值 定位下 DEBUG,数据库连上了?
=============================================
是空值不错,可是看异常是在read函数里面。
楼主你看帖子了吗?就是你的read函数没去操作strStr啊,在main函数中的strStr仍然是null啊,不能访问strStr[i]德,还是要抛异常的。
{++countLine;
strStr=br.readLine().split("\t");
}
肯定要抛异常,读到最后就是null.split("\t");
================================================
我晕
没看到while条件?
---------------------------------------
每次循环两次readLine,肯定不对了。如果是奇数行会抛null异常
while ((temp = br.readLine())!=null)
{++countLine;
strStr=temp.split("\t");
}
String str ="jdbc:microsoft:sqlserver://localhost: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;
try{
File file = new File("WINPY.TXT");
FileInputStream reader = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(reader));
String strFile = null;
int i=1;
System.out.println(1);
String temp = null;
while ((temp = br.readLine())!=null)
{++countLine;
strStr=temp.split("\t");
}
System.out.println(strStr[0]);
br.close();
reader.close();}
catch(IOException e)
{System.out.println(e.getMessage());
};
System.out.println(strStr);
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);
}
//rs.close();
stmt.close();
conn.close();我改成这样了 还是有个错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at wangsong.data.sqllink.main(sqllink.java:55)还得麻烦下
aa \t bb
aa \t bb
aa \t bb
...你要把它放进一个1维数组,通过你的程序似乎只能放最后一行建议
用2维数组,
或者1维数组,存每一行(每一行暂时不分解)你这段代码逻辑有错
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);
}
你的countLine 存的是行数,你的i是1行中的数据的位置数,i只可能为0或者是1
说的我好像明白了点,不果用二维数组怎么做?能具体点么
如果是我,我就不用数组用Vector:
先遍历文件,把每一行存到1个Vector
然后遍历Vector
{
对其中每一个元素(文件中1行)分解;(当然要注意有空行,1行1个\t ,或者空数据的问题)
组织SQL存数据库
}
这样的代码比较清晰明,但这样的代码效率不好或者干脆Vector也不用,你读一行就存一次,这样你都不用耗费内存去缓存整个文件的数据:
遍历文件(读一行){
如果此行为空,continie;//空行,属于坏数据
分解此行成为1个数组
判断数组为空或者数组中的数据有为空continue;//坏数据(当然你要考虑数组中如果有NULL的情况,这个貌似业务问题,这里很关键,多用些log吧)
组织SQL存数据库
}
这样的代码可能稍难阅读,不过内存占用会低些,遍历次数也少些。
CrazyGou(阿狗)(简单就是美) ----------------------------------------说的很明白了,你每次都读了两次,那个while判断,如果到最后一行,他判断为true,但是你循环里又读了一行,就是null,就会报错,(
while ((br.readLine())!=null)
{++countLine;
strStr=br.readLine().split("\t");
}
----
改成
while((strStr = br.readLine())!=null)
{
strStr = strStr.split("\t");
}
我也是这么觉得,换了个文件,果然就行了,虽然没有达到我读取那里面内容的目的(那里面不规范,我想没法读),但是学到了知识。谢谢各位 ,散分
}
catch(Exception e){}
用上面的!
read(strStr,countLine);是不是这错了!