Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,

i have made a code for login form and i have input details to mysql database manually, and now i want to access it in my form...
also i want to insert data as well.

this is my code,

and when i execute it, it will gives a message connection established.
and directly jump to error message and says "Before start of result set".

Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author businesssupport
 */
public class VisaDocs {

    /**
     * @param args the command line arguments
     */
public static void main(String[] args) {
// TODO code application logic here
Connection conx = null;
Statement stmt = null;
ResultSet rs = null;
//String Driver="";
String URL = "jdbc:mysql://localhost:3306/entry";
String db ="entry";
String u_name = "root";
String u_pwd="";
String query ="select * from users";
//String u_1name, u_2name;

try{       
//Class.forName("com.mysql.jdbc.Driver").newInstance();
conx = DriverManager.getConnection(URL,u_name,u_pwd);
if(conx!= null){
    JOptionPane.showMessageDialog(null,"Connection has established","I N F O",JOptionPane.INFORMATION_MESSAGE);
stmt = conx.prepareStatement(query);
rs = stmt.executeQuery(query);


     String u_1name = rs.getString("username");
     String u_2name = rs.getString("password");

JOptionPane.showMessageDialog(null,u_1name,"This is supposed to be the MessageBox title.",JOptionPane.INFORMATION_MESSAGE);
}}


catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage(),"E R R O R",JOptionPane.ERROR_MESSAGE);
}
 finally{
    try{
    conx.close();
    }catch(Exception ex){
    JOptionPane.showMessageDialog(null,ex.getMessage(),"closing err",JOptionPane.ERROR_MESSAGE);
    }
}  


also how do i show selected data in a message box.
ex : (null,'"+u_1name+"'"+u_2name+"',"This is supposed")
is it like this or any other way....
Posted

1 solution

You need to move the result set cursor to the first row, by calling rs.next()[^].
 
Share this answer
 

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