الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ربط الjframes بالكلاس الخاص بقاعدة البيانات

مغلق
بدأه smart princess في 2 أبريل 2012 · 1 رد · 774 مشاهدة · في JavaEE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

اخواني انا عملت في مشروعي كلاس منفرد للإرتباط بقاعدة البيانات mysql وهذا هو:

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;



public class StoreManegment {


 //public static void main(String[] args) {
    static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
    static final String DATABASE_URL="jdbc:mysql://localhost/store";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";
    Connection conn = null;

    public void connect() throws SQLException{
        try {
            Class.forName(JDBC_DRIVER);

            Connection con=(Connection) DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);

            //prepared statement

           System.out.println("connected successfully");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StoreManegment.class.getName()).log(Level.SEVERE, null, ex);
        }//end try/catch

    }//end connect method




}//end

والحمدلله عمل بنجاح

ولكن الآن اريد كود لربط الjframe في نفس المشروع بهذا الكود حتى استطيع استخراج البيانات من قاعدة البيانات

علما بأني قمت باستخدام هذا الكود ولكن عندما اعمل run للبرنامج الjfram الخاص باستخراج اليوزر والباسوورد من الديتابيس لا يعمل بمعنى انه ينقلني الى الjframالاخرى بادخال اي يوزر وباسوورد

private void initComponents()
    {


        try
        {
            conn = DriverManager.getConnection(StoreManegment.DATABASE_URL , StoreManegment.USERNAME , StoreManegment.PASSWORD);
            statement = conn.createStatement();
            // Result set get the result of the SQL query
            resultSet = statement.executeQuery("select * from adminstrator");
            while (resultSet.next())
            {
                int a = resultSet.getInt(1);
                float b = resultSet.getFloat(4);
                String c = resultSet.getString(2);
                String d = resultSet.getString(3);
                personne ym = new personne(a, b, c, d);
                k.add(ym);
            }

            } catch (Exception e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "error");
        }
    }
final String wrong= "Incorrect Password or username";

        if (isLoggedIn(user.getText(), pword.getText())){

            adminMain a=new adminMain();
            a.setSize(816,628);
            a.setVisible(true);
            dispose();
        }else
        {
            JOptionPane.showMessageDialog(rootPane, wrong);

        }}                                        

                boolean isLoggedIn(final String userName, final String password)
{
        String sql = "SELECT * FROM adminstrator where admin_user = ? and admin_pass = ?";
        // set parameters and execute query 
        // ResultSet rs = ....
        return true;    // if found in Database, otherwise return false;
}

فما الحل؟؟

تم تعديل هذه المشاركة بواسطة smart princess في 2 أبريل 2012 في 20:17

#2

السلام عليكم

فقط للفائده...

لقد حللت المشكله ولله الحمد وهذا هو الكود الخاص بربط ال login فريم بالديتابيس...

String sql="Select * from adminstrator";
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/store","root","root");
            Statement stmt=con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            String usr=user.getText();
            String pwd= new String (pword.getPassword());
            while(rs.next()) {
            String uname=rs.getString("admin_user");
            //Username is the coloumn name in the database table
            String password=rs.getString("admin_pass");
            if ((usr.equals(uname)) && (pwd.equals(password))){
           // new adminMain().setVisible(true);
                adminMain a= new adminMain();
                a.setSize(816,628);
                a.setVisible(true);
            }
            else{
                String wrong="wrong user name or password";

        JOptionPane.showMessageDialog(rootPane, wrong);
            }



}

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }

هذا الموضوع مغلق.

مواضيع مشابهة