Q:

Exception in MySQL

I have written a code in Java which has to populate a dot file. I am getting the error and I cant figure out why. All of the paths have been set.
Class.forName("com.mysql.jdbc.Driver");                     
Connection connects = DriverManager.getConnection("jdbc:mysql://localhost:3306/userlogin", "root","12345");
Statement stm = connects.createStatement();
ResultSet rs=stm.executeQuery("SELECT * FROM authors_4 WHERE self_authors='"+getTxt+"'");

                         int l=0;
                         /*if(rsx.next())
                         {
                             div = Double.parseDouble(rsx.getString("divs"));

                         }*/

                       do
                         {
                             if(l==0)
                             {

                                 diva = Integer.parseInt(rs.getString("tot_pub"));

                                 //fout.write("a [label=" + "\"" + rsx.getString(2) + "\\n" + rsx.getString(4) + "\"" + "color=blue,fontsize=24,fontcolor=red,style=filled];\r\n");
                                 fout.write("\"" + rs.getString("self_authors") + "\"" + "[fixedsize=true, width="+diva+",height="+diva+", label=" + "\"" + rs.getString("self_authors") + "\\n" + rs.getString("tot_pub") + "\"" + "color=blue,fontsize=24,fontcolor=red,style=filled];\r\n");
                               l++;
                             }

                             divb = Integer.parseInt(rs.getString("tot_pub")) ;

                             fout.write("\"" + rs.getString("co_auths") + "\"" + "[fixedsize=true, width="+divb+",height="+divb+",label=" + "\"" + rs.getString("co_auths") + "\\n" + rs.getString("tot_pub") + "\"];" +"\r\n");    

                             //fout.write("a ->" + "\"" + rsx.getString(6) + "\"" + "[penwidth=" +  rsx.getString(8)  +", label =" + rsx.getString(8) +" ];" + "\r\n");
                               fout.write("\"" + rs.getString("self_authors") + "\" ->" + "\"" + rs.getString("co_auths") + "\"" + "[penwidth=" +  rs.getString("1")  +", label =" + 1 +" ];" + "\r\n");

                             //System.out.println(rsx.getString(1));

                         }while(rs.next());

                         fout.write("}");
                         fout.close();
                     }

The code that I have written is very long and cant be explained so I'm only posting the portion where I am getting exceptions.
The exceptions are:
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5733) 

A:

The body of a do-while loop executes before the while expression is evaluated.
This means that your cursor will not have advanced in the first pass of the loop. You need to use a while loop:
while(rs.next()) {
   ...
}

Also consider using PreparedStatement to protect against SQL injection attacks.

