Hi, I'm new to Java and mySQL. So any help here will be appreciated. Thanks
What i'm trying to do here is to print some values from my database.
But when i tried compiling it, it showed the error statement shown below. How should i declare the
stats variable in the LoginMenu method where it would derive the
stats statement from MainMenu class?
C:\Program Files\Java\Login.java:20: cannot find symbol
symbol : method executeQuery(java.lang.String)
location: class java.lang.String
ResultSet results =
stat.executeQuery(selectQuery);
--------------------------------------------------------
Code:
import java.sql.*;
import java.util.*;
public class Login {
public void loginMenu(){
MainMenu mainMenu = new MainMenu();
String selectQuery = "Select * from userinfo";
//get the results
ResultSet results = stat.executeQuery(selectQuery) <----- Error Line
//output the results
while (results.next())
{
//example - column is called 'firstname'
System.out.println("first name: " +
results.getString("name"));
}
}
----------------------------------------------------------
Code:
import java.sql.*;
import java.util.*;
public class MainMenu {
public void dbConnection(){
try{
//create driver
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection myConnection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/battlestation","root","123");
//create statement handle for executing queries
statement stat = myConnection.createStatement();
}
catch( Exception E ) {
System.out.println( E.getMessage() );
}
}
public static void main (String[]args){
MainMenu mainMenu = new MainMenu();
mainMenu.dbConnection();
}
}
---------------------------------------------------------------