0

我是 Android 编程新手;我制作了几个关于简单表的程序,这些程序仅适用于“从数据库中获取数据并将其显示在表上”或“将 jtextfields 中的文本保存到数据库”。所以我需要在 Android 上执行此操作,但我认为我的知识在 Android 上根本不起作用。我应该如何在 Android 上执行此操作?

import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class YeniUye extends javax.swing.JFrame {

    Connection conn=null;
    ResultSet rs=null;
    PreparedStatement pst=null;



    /**
     * Creates new form YeniUye
     */
    public YeniUye() {
        initComponents();
    conn=javaconnect.ConnecrDb();
         Update_table();




    }

    private void Update_table(){


    try{
    String sql ="select * from YeniUyeFrame ";
   pst=conn.prepareStatement(sql);
   rs=pst.executeQuery();
   yeniuyetable.setModel(DbUtils.resultSetToTableModel(rs));


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


}finally{
try{
pst.close();
rs.close();
}catch(Exception e){
}
}





}    




    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        yeniuyeepostatxt = new javax.swing.JTextField();
        yeniuyeyastxt = new javax.swing.JTextField();
        yeniuyeisimtxt = new javax.swing.JTextField();
        yeniuyekatilimtarihitxt = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        yeniuyetable = new javax.swing.JTable();
        cmd_save = new javax.swing.JButton();
        cmd_save1 = new javax.swing.JButton();
        cmd_save2 = new javax.swing.JButton();
        cmd_save3 = new javax.swing.JButton();
        cmd_save4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Yaş");

        jLabel1.setText("İsim");

        jLabel4.setText("E-Posta");

        jLabel5.setText("Katılım Tarihi");

        yeniuyeepostatxt.setBackground(new java.awt.Color(153, 255, 153));

        yeniuyeyastxt.setBackground(new java.awt.Color(153, 255, 153));

        yeniuyeisimtxt.setBackground(new java.awt.Color(153, 255, 153));
        yeniuyeisimtxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yeniuyeisimtxtActionPerformed(evt);
            }
        });

        yeniuyekatilimtarihitxt.setBackground(new java.awt.Color(153, 255, 153));

        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()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(yeniuyeyastxt, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(yeniuyeisimtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(yeniuyeepostatxt, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(yeniuyekatilimtarihitxt, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(yeniuyeisimtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yeniuyeyastxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yeniuyeepostatxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(yeniuyekatilimtarihitxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        yeniuyetable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        yeniuyetable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                yeniuyetableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(yeniuyetable);

        cmd_save.setBackground(new java.awt.Color(0, 0, 0));
        cmd_save.setForeground(new java.awt.Color(255, 255, 255));
        cmd_save.setText("Kaydet");
        cmd_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_saveActionPerformed(evt);
            }
        });

        cmd_save1.setBackground(new java.awt.Color(0, 0, 0));
        cmd_save1.setForeground(new java.awt.Color(255, 255, 255));
        cmd_save1.setText("Sil");
        cmd_save1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_save1ActionPerformed(evt);
            }
        });

        cmd_save2.setBackground(new java.awt.Color(0, 0, 0));
        cmd_save2.setForeground(new java.awt.Color(255, 255, 255));
        cmd_save2.setText("Temizle");
        cmd_save2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_save2ActionPerformed(evt);
            }
        });

        cmd_save3.setBackground(new java.awt.Color(0, 0, 0));
        cmd_save3.setForeground(new java.awt.Color(255, 255, 255));
        cmd_save3.setText("Düzenle");
        cmd_save3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_save3ActionPerformed(evt);
            }
        });

        cmd_save4.setBackground(new java.awt.Color(0, 0, 0));
        cmd_save4.setForeground(new java.awt.Color(255, 255, 255));
        cmd_save4.setText("Ana Menü");
        cmd_save4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_save4ActionPerformed(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()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmd_save2)
                            .addComponent(cmd_save))
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmd_save1)
                            .addComponent(cmd_save3)))
                    .addComponent(cmd_save4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd_save)
                    .addComponent(cmd_save1))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd_save3)
                    .addComponent(cmd_save2))
                .addGap(59, 59, 59)
                .addComponent(cmd_save4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
                .addContainerGap())
        );

        setBounds(0, 0, 866, 538);
    }// </editor-fold>                        

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

    private void yeniuyetableMouseClicked(java.awt.event.MouseEvent evt) {                                          

        try{
         int row =yeniuyetable.getSelectedRow();
         String Table_click=(yeniuyetable.getModel().getValueAt(row, 0).toString());
         String sql ="select * from YeniUyeFrame where İsim='"+Table_click+"' ";
         pst=conn.prepareStatement(sql);
         rs=pst.executeQuery();
         if(rs.next()){

         String add1 =rs.getString("İsim");
         yeniuyeisimtxt.setText((rs.getString("İsim")).trim());

         String add2 =rs.getString("Yaş");
         yeniuyeyastxt.setText((rs.getString("Yaş")).trim());

         String add3 =rs.getString("EPosta");
         yeniuyeepostatxt.setText((rs.getString("EPosta")).trim());

         String add4 =rs.getString("KatılımTarihi");
         yeniuyekatilimtarihitxt.setText((rs.getString("KatılımTarihi")).trim());






         }         


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



        }finally{
try{
pst.close();
rs.close();
}catch(Exception e){
}
}














    }                                         

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

        try{
            String sql = "Insert into YeniUyeFrame (İsim,Yaş,EPosta,KatılımTarihi) values (?,?,?,?)";

            pst=conn.prepareStatement(sql);
            pst.setString(1, yeniuyeisimtxt.getText());
            pst.setString(2, yeniuyeyastxt.getText());
            pst.setString(3, yeniuyeepostatxt.getText());
            pst.setString(4, yeniuyekatilimtarihitxt.getText());


            pst.executeUpdate();

            JOptionPane.showMessageDialog(null, "Kaydedildi!");

        }

        catch(Exception e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e);

        }finally{
try{
pst.close();
rs.close();
}catch(Exception e){
}
}

        Update_table();

    }                                        

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

         String sql="delete from YeniUyeFrame where İsim =?";



    try{

    pst=conn.prepareStatement(sql);

    pst.setString(1, yeniuyeisimtxt.getText());

    pst.executeUpdate();

    JOptionPane.showMessageDialog(null, "Silindi");


    }catch(Exception e){

    JOptionPane.showMessageDialog(null, e);


    }

    Update_table();








    }                                         

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

        yeniuyeisimtxt.setText("");
      yeniuyeyastxt.setText("");
      yeniuyeepostatxt.setText("");
      yeniuyekatilimtarihitxt.setText("");


    }                                         

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

        try{

String value1=yeniuyeisimtxt.getText();
String value2=yeniuyeyastxt.getText();
String value3=yeniuyeepostatxt.getText();
String value4=yeniuyekatilimtarihitxt.getText();




 String sql="update YeniUyeFrame set İsim='"+value1+"' ,Yaş ='"+value2+"',EPosta='"+value3+"',KatılımTarihi='"+value4+"' where İsim='"+value1+"' ";
              pst=conn.prepareStatement(sql);
              pst.executeUpdate();
              JOptionPane.showMessageDialog(null, "Güncellendi");



}catch(Exception e){;

        JOptionPane.showMessageDialog(null, e);


}finally{
try{
pst.close();
rs.close();
}catch(Exception e){
}
}
  Update_table();









    }                                         

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

         dispose();
        SeperateMenu s = new SeperateMenu();
         s.setVisible(true);









    }                                         

    /**
     * @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(YeniUye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(YeniUye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(YeniUye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(YeniUye.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 YeniUye().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton cmd_save;
    private javax.swing.JButton cmd_save1;
    private javax.swing.JButton cmd_save2;
    private javax.swing.JButton cmd_save3;
    private javax.swing.JButton cmd_save4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField yeniuyeepostatxt;
    private javax.swing.JTextField yeniuyeisimtxt;
    private javax.swing.JTextField yeniuyekatilimtarihitxt;
    private javax.swing.JTable yeniuyetable;
    private javax.swing.JTextField yeniuyeyastxt;
    // End of variables declaration                   


}
4

1 回答 1

0

相当于安卓JPanel is LinearLayout

equivilent of JButton is Button
JLabel -> TextView
JScrollPane -> ScrollView

JOptionPane -> AlertDialog or Spinner or Dialog取决于你的应用做什么

等价于ResultSet is Cursor-- 与 ResultSet 使用完全相同

例子:

Cursor c = database.query("SELECT * FROM someTable");
int someValue = c.getInt(someTableColumnIndex);
String someStringValue = c.getString(someTableOtherColumnIndex);

查找:http: //developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

但是您的代码需要进行重大改写以使其在 Android 上看起来相同

Android 主要使用 XML 布局,但您可以使用代码来创建布局

例子 :

public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
LinearLayout layout = new LinearLayout(this,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); //width and height

Button button = new Button(this);
button.setText("hello");
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this,"you clicked me! my text is: "+(Button)v.getText().toString(),Toast.LENGTH_LONG).show();
}
});
}

}

如果你想建立一个表 - Android 有 TableLayout 看看这个:http: //developer.android.com/reference/android/widget/TableLayout.html

于 2013-09-21T10:01:35.653 回答