// You need to import the java.sql package to use JDBC import java.sql.*; public class JDBCTest { public static void main ( String[] args ) { try{ int userId= login("olle","olle"); System.out.println("User id= "+userId); }catch(Exception e){ e.printStackTrace(); } } public static int login(String username, String pw) throws SQLException,ArrayStoreException { System.out.println(" 1. Load the MySQL JDBC driver"); DriverManager.registerDriver(new org.gjt.mm.mysql.Driver()); System.out.println(" 2. Get the connection."); Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/gnomeshop?user=gnomeshop&password=gnomeshop" ); System.out.println(" 3. Create a statement."); Statement stmt = conn.createStatement (); System.out.println(" 4. Do the SQL."); String sql= "SELECT USER_ID FROM USER WHERE IS_BANNED='N' AND USER_NAME='"+username+"' AND PW='"+ pw +"'"; ResultSet r = stmt.executeQuery ( sql ); int userId=0; if (r.next ()){ System.out.println(" 5. Success."); userId = r.getInt(1); }else{ System.out.println(" 5. Not allowed to login."); userId= -1; } System.out.println(" 6. Close the result set, the statement and connect."); r.close(); stmt.close(); conn.close(); return userId; } }