2012-12-20 2 views
2

netbeans를 사용하여 자바 로그인 프레임을 만들었고 jar 파일이 프로젝트 라이브러리에 추가 된 MySQL Connector/J를 사용하여 MySQL에 연결했습니다. 또한 모든 로그인 세부 정보를 포함하는 login이라는 테이블을 만들었습니다. 다음 코드는 로그인을 허용해야하지만 데이터베이스 연결이 설정되지 않은 것처럼 오류가 계속 발생합니다.mysql에 연결된 java로 로그인 폼을 생성

package Lightapp; 
import java.sql.* ; 
import javax.swing.* ; 

public class AbbeyLog extends javax.swing.JFrame 
{ 
    Connection conn = null; 
    ResultSet rs = null; 
    PreparedStatement pst = null; 

    /** 
    * Creates new form AbbeyLog 
    */ 
    public AbbeyLog() 
    { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    private void textuserActionPerformed(java.awt.event.ActionEvent evt) 
    { 
     // TODO add your handling code here: 
     String sql = "select * from login where username = ? and password = ?"; 
     try 
     { 
      pst = conn.prepareStatement(sql); 
      pst.setString(1, textuser.getText()); 
      pst.setString(2, textpass.getText()); 
      rs = pst.executeQuery(); 
      if (rs.next()) 
      { 
       JOptionPane.showMessageDialog(null, "Username and Password correct"); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, "invalid username and password"); 
      } 
     } 
     catch (Exception e) 
     { 
      JOptionPane.showMessageDialog(null, e); 
     } 
    } 

    private void textuserMouseClicked(java.awt.event.MouseEvent evt) 
    { 
     // TODO add your handling code here: 
    } 


    public static void main(String args[]) 
    { 
     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new AbbeyLog().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextField textpass; 
    private javax.swing.JButton textuser; 
    // End of variables declaration 
} 
+1

당신이 준 연결 정보를? 같은 서버 이름, 데이터베이스 이름 등? –

답변

2

당신은 시작에서 JDBC 연결을 작성해야합니다

//load the driver 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
// create a connection   
conn = DriverManager.getConnection("jdbc:mysql://"+hostName+":" 
       + dbPort+"/"+databaseName+"?"+"user="+dbUser+"&password=" + dbPassword); 
+0

@ stefan, 정확히 위의 코드를 어디에 배치합니까 – lee

+0

ive 그것을 시도했지만 thos 오류가 계속 Java.sql.sqlexception : 적절한 드라이버를 찾지 못했습니다 – lee

+0

1. 시작할 때 호출하는 생성자 또는 메서드에 넣습니다. 당신의 고객을 위로하십시오. 2. mysql connector/j (mysql-connector-java-5.1.22-bin.jar)가 프로젝트 (하위 폴더 "libraries")에 있는지 확인하십시오. 자세한 내용은 여기를 참조하십시오 : http://dev.mysql.com/doc/refman/5.1/en/connector-j-usagenotes-connect-drivermanager.html – Stefan

0

그것을 할 수있는 좋은 방법은 eample에 대한 dbConnection라는 구분 된 파일을 만드는 것입니다. 이 파일에는 DB에 연결하는 메소드가 들어 있습니다. 일반적으로는 다음과 같이 표시됩니다

public class DBConnection{ 

    Connection con = null; 
    Statement stmt = null; 
    ResultSet rs = null; 

public DBConnection() {} 

public void connect() { 
    try { 
     Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
     con=DriverManager.getConnection("jdbc:jtds:sqlserver://adress:port;DatabaseName=dbName", "login", "passwd"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

을 다음 주 수업 시간에 당신이 할 :

dbConnection db=new dbConnection(); 
db.connect; 
+0

ive 그것을했지만 당신은 자바 오류 .sqlexception : 적절한 드라이버를 찾지 못했습니다. – lee

+0

드라이버에 해당하는 jar 파일이 있어야합니다. 당신이 올바른 폴더에 있고 여전히 작동하지 않는다면 다른 드라이버 (예 : sourceforge에서 제안한 드라이버)로 시도해 볼 수 있습니다 (내 게시물 참조) – Alucard