tags:

views:

23

answers:

1

I'm using the NetBeans IDE(6.8). I have a DB class :

package garits;
import java.io.Serializable;
import java.sql.*;
import java.util.Properties;
public class DB implements Serializable{

  private static final long serialVersionUID = 1L;
  String dbURL = "jdbc:mysql:///team_project";
  String user = "root";
  String pwd = "arsenal";
  String dbDriver = "com.mysql.jdbc.Driver";
  private Connection dbCon;
  private ResultSet r;
  private Statement s;

  public DB()
      {}

  public boolean connect() throws ClassNotFoundException,SQLException{
          Class.forName(dbDriver);
          Properties props = new Properties();
          props.put(user, "root");
          props.put(pwd, "arsenal");
          props.put("charSet", "UTF-8");
          props.put("lc_ctype", "UTF-8");
          dbCon = DriverManager.getConnection(dbURL,props);
          //dbCon = DriverManager.getConnection(dbURL,user,pwd);
          return true;
  }

  public void close() throws SQLException{
        dbCon.close();
  if(r!=null)
   r.close();
  if(s!=null)
   s.close();
  }

  public ResultSet execSQL(String sql) throws SQLException{

                    s = dbCon.createStatement();
                    r = s.executeQuery(sql);
                    return (r == null) ? null : r;
  }

  public int updateSQL(String sql) throws SQLException{
                   s = dbCon.createStatement();
                   int r = s.executeUpdate(sql);
                   return (r == 0) ? 0 : r;
  }

  public int updateSQL(String sql, String getID) throws SQLException{
      s = dbCon.createStatement();
      int autoIncValue = -1;
      s.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
      ResultSet rs = s.getGeneratedKeys();
      if (rs.next()) {
          autoIncValue = rs.getInt(1);
      }
      return autoIncValue;
  }
}

The jar file is im my library, but whenever I try to connect:

 private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
       String result ="";
        DB db = new DB();
       try{
           db.connect();
           String query = "Select Role From User_Account Where Username=jTextField1.getText()AND Where Password=jTextField2.getText(); ";
           ResultSet rs=db.execSQL(query);
          while(rs.next())
          {
              result = rs.getString("Role");

          }
           if(result.equals(""))
           {
               JOptionPane.showMessageDialog(loginButton,"Access denied","Error Message",JOptionPane.ERROR_MESSAGE);

           }
           else if(result.equals("Administrator"))
           {
               MainPage_Admin admin = new MainPage_Admin();
           }

       }
           catch(Exception e)
           {
            System.out.println("An error has occurred");
           }



    }  

I get an error(the exception is caught)-the name of the database is "team_project" and password is "arsenal"-any ideas appreciated. I'm new to JDBC.

+1  A: 

First step: use at least e.printStackTrace() in your catch-block to get some information from the exception. Otherwise you'll just be guessing.

Joachim Sauer
I get:java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) Why am I being denied access-thanks.
@eli: because you don't set the user/password properties correctly. Check the two lines that put `user` and `pwd` into the `Properties`: it's wrong.
Joachim Sauer