JDBC连接问题

时间:2010-03-26 11:07:54

标签: jdbc

我正在使用NetBeans IDE(6.8)。我有一个DB类:

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;
  }
}

jar文件是我的库,但每当我尝试连接时:

 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");
           }



    }  

我收到错误(异常被捕获) - 数据库的名称是“team_project”,密码是“arsenal” - 任何想法都赞赏。我是JDBC的新手。

2 个答案:

答案 0 :(得分:1)

第一步:在catch-block中使用至少 e.printStackTrace()从异常中获取一些信息。否则你只是在猜测。

答案 1 :(得分:0)

MySQL数据库url连接属性错误

jdbc:mysql://localhost:3306/<your database name>

而不是你正在给予

jdbc:mysql:///team_project

修改并执行程序,最好在try / catch块中处理异常而不是throws。