错误:解析时已到达文件末尾
问题描述:
hai,我开发了一个具有数据库连接的程序.在我的代码中,我尝试插入数据.对于数据库连接来说不是问题,但是我的代码存在问题,文件解析结束时出现错误.请更正我的代码,因为我可以确定问题是什么.我还突出显示了行,它给了我这个错误..
hai , i am developed a program with database connection. In my code i am try to insert data. For database connection is no probleam but i have probleam with my code which i got an error reached end of file parsing . Please correct my code please cause i can identify what is the probleam is. I also highlight line which give me this error..
import javax.swing.JOptionPane;
import java.sql.*;
public class ActInsertData {
String userid="saya", password="1234";
String url="jdbc:odbc:Semester2";
Statement stmt;
Connection con;
public void getConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("classNotFoundException:");
System.err.println(e.getMessage());
}
try
{
con=DriverManager.getConnection(url,userid, password);
if(con!= null)
{
System.out.println("Got Connection.");
DatabaseMetaData meta=con.getMetaData();
System.out.println("Driver Name: " + meta.getDriverName());
}
else
System.out.println("Could not Get Connection");
}
catch(SQLException ex)
{
System.err.println("SQLException: " +ex.getMessage());
}
}
public void insertData()
{
String insertString1 = "insert into student values('ALI', 15, 'MALE' )";
String insertString2 = "insert into student values('AHMAD', 16, 'MALE')";
String insertString3 = "insert into student values('AYU, 15,'FEMALE')";
String insertString4 = "insert into student values('ANI', 17,'FEMALE')";
try
{
stmt = con.createStatement();
stmt.executeUpdate(insertString1);
stmt.executeUpdate(insertString2);
stmt.executeUpdate(insertString3);
stmt.executeUpdate(insertString4);
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
System.out.println("Data Inserted into Student Table");
public static void main(String[] args) throws Exception/* this is the line which cause an error*/
{
ActInsertData obj = new ActInsertData();
obj.getConnection();
obj.insertData();
}
}
答
,当函数public static void main(String[] args)
出现问题时-则不要启动它.问题解决了.
或更确切地说,没有弄清楚.
上面的代码对文件的解析一无所知.
解析文件时,您始终需要检查是否还有其他要解析的内容:
when the functionpublic static void main(String[] args)
is the problem - then you should not start it. Problem solved.
Or rather not figured out correctly.
Your code above says nothing about the parsing of a file.
When you''re parsing a file you always need to check whether there is more to parse:
try {
BufferedReader oReader = new BufferedReader(oFile);
String strLine;
while (true) {
strLine = oReader.readLine()
if(strLine==null) break; // nothing to parse -> Job done, exit loop
process(strLine);
}
oReader.close();
}
catch (IOException e) {
}