1.11M Members

problem with Database to access Server/ServerSocket

 
0
 

Good To Meet you All!

Sir In my server

This is the JButton action for start server

private void strsrvActionPerformed(java.awt.event.ActionEvent evt) {                                         
    getClint();
}

This is action Performed sir..,

public void getClint()
{


                try
                {
            ServerSocket ss=new ServerSocket(1030);


                        try
                        {
                            JOptionPane.showMessageDialog(null,"Server is runing");
                        while(true)
                   {
                       Socket s=ss.accept();
                       Service a=new Service(s);
                       a.start();
                   }
                        }
                        catch(Exception e)
                        {
                              System.out.println(e);
                }
            }

        catch(IOException ex)
        {
            Logger.getLogger(MainF.class.getName()).log(Level.SEVERE, null,ex);
        }
}

This Is the Inner class code for to access clients

public class Service extends Thread 
{
        public Socket s;
        public Service(Socket t) 

        {
             s=t;
        }
        public void run()
        {
            try
    {
         BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter output = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                String userName = input.readLine();
                String password = input.readLine();
                String getd=input.readLine();
                try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip", "root", "root");
        PreparedStatement ps = conn.prepareStatement("select * from employeenew where un='"+userName+"' and pw='"+password+"'");
         ps.execute();
       while(true)
        {
            PreparedStatement ps1 = conn.prepareStatement("insert into attents (un='"+userName+"',pw='"+password+"',intim='"+getd+"')");
            output.println("You Have LogIn" + userName);
            ps1.executeUpdate();
             conn.close();
          }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane,"LoginFailed");
    }
                output.close();
                input.close();
                s.close();
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
                }
                }

The client code is

a/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Client.java
 *
 * Created on Apr 28, 2012, 10:58:31 AM
 */

import java.io.*;
import java.net.*;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
/**
 *
 * @author e-soft
 */
public class Client extends javax.swing.JFrame {
Socket s;
    /** Creates new form Client */
    public Client() {
        initComponents();
    }
        /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        un = new javax.swing.JTextField();
        pw = new javax.swing.JTextField();
        lin = new javax.swing.JButton();
        lo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lin.setText("LogIn");
        lin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                linMousePressed(evt);
            }
        });
        lin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linActionPerformed(evt);
            }
        });

        lo.setText("LogOut");
        lo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pw, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                        .addComponent(lo)
                        .addGap(79, 79, 79))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lin)
                    .addComponent(lo))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

private void linActionPerformed(java.awt.event.ActionEvent evt) {                                    
// TODO add your handling code here:
         try
        {
      Socket s=new Socket("192.168.1.8",1030);
      PrintWriter output = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
      String userName =un.getText();
      output.println(userName);
      String password =pw.getText();
      output.println(password);
      Date dt=Calendar.getInstance().getTime();
      Date timzn=(dt);
      output.println(timzn);
      output.flush();
      BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
      String response = input.readLine();
      JOptionPane.showMessageDialog(rootPane, response);
      output.close();
      input.close();
      s.close();
      lo.setEnabled(true);
      lin.setEnabled(false);
       }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(rootPane, "Server Is Not In Conection");

       }

}                                   

private void loActionPerformed(java.awt.event.ActionEvent evt) {                                   
// TODO add your handling code here:
    lin.setEnabled(true);
    lo.setEnabled(false);
}                                  

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

}                                

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

               /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Client().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton lin;
    private javax.swing.JButton lo;
    private javax.swing.JTextField pw;
    private javax.swing.JTextField un;
    // End of variables declaration                   
}

My problem is
the server can use the username and password as static declaration only

But

if i connect to database it showing error while before access the database,
(even it not access the database try Block)
and the error message also unable to read, it showing transparent-able or black screen sir..,

(I Don't know how to properly use the database with server)

Anyone can help me please..,
Thank you..,

 
0
 

Cross posted at Click Here

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article