Click here to Skip to main content
15,563,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..

Java
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();
}
}
Posted
Updated 28-Sep-11 0:35am
v2

1 solution

when the function public 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:
Java
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) {

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900