在网站中部署使用swing创建的表单

时间:2013-11-19 07:56:29

标签: java swing web

我使用java swing设计了一个表单。但我不知道如何在网站中部署它。我创建的表单是“.java”扩展名,如何在网页中部署它?我真的很新web development.i知道这可能是一个奇怪的问题,但我仍然找不到我想要的东西,所以这就是为什么我在这里问这个。如果有人可以帮助我。我已经给了我的部分代码

package Students;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;



/**
 *
 * @author user
 */
public class student extends javax.swing.JFrame {
    Connection con;
Statement stmt;
ResultSet rs;
int curRow = 0;

    /** Creates new form student */
    public student() {
        initComponents();
        DoConnect();

    }

public void DoConnect( ) {
    try{
        //connect to a database
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");

        //Execute some sql and load the recrds into result set
        stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
        String sql="SELECT * FROM students";
        rs=stmt.executeQuery(sql);

        rs.next();
        int id_col=rs.getInt("ID");
        String id=Integer.toString(id_col);
        String name=rs.getString("Name");
        String address=rs.getString("Address");
        String course=rs.getString("Course");
        String semester=rs.getString("Semester");


        //Display the first record in the text fields
         jTextField1.setText(id);
         jTextField2.setText(name);
         jTextField3.setText(address);
         jTextField4.setText(course);
          jTextField5.setText(semester);
    }
    catch(SQLException err){
        JOptionPane.showMessageDialog(student.this,err.getMessage());




    }

}
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        btnFirst = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnNewRecord = new javax.swing.JButton();
        btnDeleteRecord = new javax.swing.JButton();
        btnSaveRecord = new javax.swing.JButton();
        btnCancelNewRecord = new javax.swing.JButton();
        btnUpdateRecord = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.setText("id");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText("ID");

        jLabel2.setText("Name");

        jLabel3.setText("Address");

        jLabel4.setText("Course");

        jLabel5.setText("Semester");

        jTextField2.setText("name");

        jTextField3.setText("add");

        jTextField4.setText("cs");

        jTextField5.setText("sem");

        btnPrevious.setText("Next");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        btnNext.setText("Previous");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnLast.setText("First");
        btnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLastActionPerformed(evt);
            }
        });

        btnFirst.setText("Last");
        btnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFirstActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnPrevious, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLast, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                        .addContainerGap(18, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnNext, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(btnFirst, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                        .addGap(18, 18, 18))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btnLast)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPrevious)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFirst)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        btnNewRecord.setText("New Record");
        btnNewRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewRecordActionPerformed(evt);
            }
        });

        btnDeleteRecord.setText("Delete Record");
        btnDeleteRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteRecordActionPerformed(evt);
            }
        });

        btnSaveRecord.setText("Save New Record");
        btnSaveRecord.setEnabled(false);
        btnSaveRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveRecordActionPerformed(evt);
            }
        });

        btnCancelNewRecord.setText("Cancel New Record");
        btnCancelNewRecord.setEnabled(false);
        btnCancelNewRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelNewRecordActionPerformed(evt);
            }
        });

        btnUpdateRecord.setText("Update Record");
        btnUpdateRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateRecordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnUpdateRecord, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                    .addComponent(btnNewRecord, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnDeleteRecord, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancelNewRecord, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSaveRecord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(btnNewRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDeleteRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSaveRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelNewRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnUpdateRecord)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

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

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

    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {                                         
 try {
        rs.first( ) ;
        int id_col=rs.getInt("ID");
        String id=Integer.toString(id_col);
        String name=rs.getString("Name");
        String address=rs.getString("Address");
        String course=rs.getString("Course");
        String semester=rs.getString("Semester");


        //Display the first record in the text fields
         jTextField1.setText(id);
         jTextField2.setText(name);
         jTextField3.setText(address);
         jTextField4.setText(course);
          jTextField5.setText(semester);

}
catch (SQLException err) {
JOptionPane.showMessageDialog(student.this, err.getMessage());       // TODO add your handling gcode here:
    }                                        
    }
    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
try {
if ( rs.next( ) ) {
    int id_col=rs.getInt("ID");
        String id=Integer.toString(id_col);
        String name=rs.getString("Name");
        String address=rs.getString("Address");
        String course=rs.getString("Course");
        String semester=rs.getString("Semester");


        //Display the first record in the text fields
         jTextField1.setText(id);
         jTextField2.setText(name);
         jTextField3.setText(address);
         jTextField4.setText(course);
          jTextField5.setText(semester);

}
else {
rs.previous( );
JOptionPane.showMessageDialog(student.this, "First of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(student.this, err.getMessage());

}        // TODO add your handling code here:
    }                                       

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                            
       try {
if ( rs.previous( ) ) {
    int id_col=rs.getInt("ID");
        String id=Integer.toString(id_col);
        String name=rs.getString("Name");
        String address=rs.getString("Address");
        String course=rs.getString("Course");
        String semester=rs.getString("Semester");


        //Display the first record in the text fields
         jTextField1.setText(id);
         jTextField2.setText(name);
         jTextField3.setText(address);
         jTextField4.setText(course);
          jTextField5.setText(semester);

}
else {
rs.next( );
JOptionPane.showMessageDialog(student.this, "End of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(student.this, err.getMessage());

} // TODO add your handling code here:
    }                                           

    private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {                                        
 try {
        rs.last( ) ;
        int id_col=rs.getInt("ID");
        String id=Integer.toString(id_col);
        String name=rs.getString("Name");
        String address=rs.getString("Address");
        String course=rs.getString("Course");
        String semester=rs.getString("Semester");


        //Display the first record in the text fields
         jTextField1.setText(id);
         jTextField2.setText(name);
         jTextField3.setText(address);
         jTextField4.setText(course);
          jTextField5.setText(semester);

}
catch (SQLException err) {
JOptionPane.showMessageDialog(student.this, err.getMessage());       // TODO add your handling gcode here:
    }        // TODO add your handling code here:
    }                                       

    private void btnNewRecordActionPerformed(java.awt.event.ActionEvent evt) {                                             
btnFirst.setEnabled( false );
btnPrevious.setEnabled( false ) ;
btnNext.setEnabled( false );
btnLast.setEnabled( false );
btnUpdateRecord.setEnabled( false );
btnDeleteRecord.setEnabled( false );
btnNewRecord.setEnabled( false );

btnSaveRecord.setEnabled( true );
btnCancelNewRecord.setEnabled( true );
try{
    curRow = rs.getRow( );
     jTextField1.setText("");
         jTextField2.setText("");
         jTextField3.setText("");
         jTextField4.setText("");
          jTextField5.setText("");
}catch(SQLException err){
    System.out.println(err.getMessage());
}

    }                                            

    private void btnSaveRecordActionPerformed(java.awt.event.ActionEvent evt) {                                              
JOptionPane.showMessageDialog(student.this, "Saved !");
btnFirst.setEnabled( true );
btnPrevious.setEnabled( true ) ;
btnNext.setEnabled( true );
btnLast.setEnabled( true );
btnUpdateRecord.setEnabled( true );
btnDeleteRecord.setEnabled( true );
btnNewRecord.setEnabled( true );

btnSaveRecord.setEnabled( false );
btnCancelNewRecord.setEnabled( false );
String name = jTextField2.getText( );
String address =jTextField3.getText( );
String course = jTextField4.getText( );
String ID =jTextField1.getText();
String semester=jTextField5.getText();
int newID = Integer.parseInt( ID );
try{
    rs.moveToInsertRow();


    rs.updateInt("ID", newID);
    rs.updateString("Name", name);
    rs.updateString("Address", address);
    rs.updateString("Course", course);
    rs.updateString("Semester", semester);
    rs.insertRow();
    stmt.close();

    rs.close();
    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

String sql = "SELECT * FROM students";
rs = stmt.executeQuery(sql);

rs.next( );
int id_col = rs.getInt("ID");
String id=Integer.toString(id_col);
        String name1=rs.getString("Name");
        String address1=rs.getString("Address");
        String course1=rs.getString("Course");
        String semester1=rs.getString("Semester");

 jTextField1.setText(id);
         jTextField2.setText(name1);
         jTextField3.setText(address1);
         jTextField4.setText(course1);
          jTextField5.setText(semester1);

        }catch(SQLException err){
            System.out.println(err.getMessage());
        }// TODO add your handling code here:
    }                                             

    private void btnUpdateRecordActionPerformed(java.awt.event.ActionEvent evt) {                                                

String name = jTextField2.getText( );
String address =jTextField3.getText( );
String course = jTextField4.getText( );
String ID =jTextField1.getText();
String semester=jTextField5.getText();
int newID = Integer.parseInt( ID );
try {
rs.updateInt( "ID", newID );
rs.updateString( "Name", name );
rs.updateString( "Address", address );
rs.updateString( "Course", course );
rs.updateString("Semester", semester);
rs.updateRow( );
JOptionPane.showMessageDialog(student.this, "Updated");

}
catch (SQLException err) {
System.out.println(err.getMessage() );
}
// TODO add your handling code here:
    }                                               

    private void btnCancelNewRecordActionPerformed(java.awt.event.ActionEvent evt) {                                                   
btnFirst.setEnabled( true );
btnPrevious.setEnabled( true ) ;
btnNext.setEnabled( true );
btnLast.setEnabled( true );
btnUpdateRecord.setEnabled( true );
btnDeleteRecord.setEnabled( true );
btnNewRecord.setEnabled( true );

btnSaveRecord.setEnabled( false );
btnCancelNewRecord.setEnabled( false );
try{
rs.absolute( curRow );

          jTextField1.setText(rs.getString("ID"));
         jTextField2.setText(rs.getString("Name"));
         jTextField3.setText(rs.getString("Address"));
         jTextField4.setText(rs.getString("Course"));
          jTextField5.setText(rs.getString("Semester"));


        }catch(SQLException err){
            System.out.println(err.getMessage());
        }// TODO add your handling code here:
    }                                                  

    private void btnDeleteRecordActionPerformed(java.awt.event.ActionEvent evt) {                                                
try{
    rs.deleteRow();
    stmt.close();
    rs.close();
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String sql="SELECT * FROM students";
    rs=stmt.executeQuery(sql);
    rs.next();
    int col_id=rs.getInt("ID");
    String id=Integer.toString(col_id);
    String name=rs.getString("Name");
    String address=rs.getString("Address");
    String course=rs.getString("Course");
    String semester=rs.getString("Semester");
 jTextField1.setText(rs.getString(id));
         jTextField2.setText(rs.getString(name));
         jTextField3.setText(rs.getString(address));
         jTextField4.setText(rs.getString(course));
          jTextField5.setText(rs.getString(semester));
        }catch(SQLException err)
{
            System.out.println(err.getMessage());
}
// TODO add your handling code here:
    }                                               

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new student().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCancelNewRecord;
    private javax.swing.JButton btnDeleteRecord;
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNewRecord;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnSaveRecord;
    private javax.swing.JButton btnUpdateRecord;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;


}

1 个答案:

答案 0 :(得分:2)

您可以将JApplet用于此目的并embedded it to a page