2011-12-22 98 views
1

在下面的代碼中,我有一個名爲Prj_Genrate_idAssgn的方法,其形式參數爲String table, String Field_Name。我怎樣才能將數據庫表名稱(studentrecords)和字段名稱(Reg_no)作爲參數(String table, String Field_name)?如何將表和字段名稱作爲數據庫查詢參數傳遞?

import java.awt.event.*; 
import javax.swing.*; 
import java.sql.*; 

public class dbtable2 extends javax.swing.JFrame implements ActionListener { 

    JFrame frame; 
    // 
    JLabel lname = new JLabel("STUDENT NAME"); 
    JLabel lreg = new JLabel("REGISTER NO"); 
    JLabel lmark1 = new JLabel("MARK1"); 
    JLabel lmark2 = new JLabel("MARK2"); 
    JLabel ltotal = new JLabel("TOTAL"); 
    JButton bsave = new JButton("SAVE"); 
    JButton bupdate = new JButton("UPDATE"); 
    JButton bdelete = new JButton("DELETE"); 
    JTextField tname = new JTextField(20); 
    JTextField treg = new JTextField(20); 
    JTextField tmark1 = new JTextField(20); 
    JTextField tmark2 = new JTextField(20); 
    JTextField ttotal = new JTextField(20); 
    Connection conn = null; 
    CallableStatement calstat = null; 
    Statement st = null; 
    ResultSet rs = null; 
    static String str = "table"; 
    static String str1 = "Field_Name"; 
    PreparedStatement pr = null; 

    public dbtable2() { 

     //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame = new JFrame(); 
     tmark1.addKeyListener(new TxAdapter()); 
     tmark2.addKeyListener(new TxAdapter()); 
     bsave.addActionListener(this); 
     bupdate.addActionListener(this); 
     bdelete.addActionListener(this); 

     JPanel pnl = new JPanel(); 
     pnl.add(lname); 
     pnl.add(tname); 
     pnl.add(lreg); 
     pnl.add(treg); 
     pnl.add(lmark1); 
     pnl.add(tmark1); 
     pnl.add(lmark2); 
     pnl.add(tmark2); 
     pnl.add(ltotal); 
     pnl.add(ttotal); 
     pnl.add(bsave); 
     pnl.add(bupdate); 
     pnl.add(bdelete); 
     frame.add(pnl); 
     frame.setSize(200, 100); 
     frame.setVisible(true); 

     initconn(); 

    } 

    public Connection initconn() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      conn = DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/records", "root", "root"); 
      String table = "CREATE TABLE studentrecord(student_name varchar(20)," 
       + "Reg_no int(6) PRIMARY KEY,mark1 int(3), mark2 int(3))"; 
      st = conn.createStatement(); 
      //st.executeUpdate(table); 
      //conn.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     return conn; 
    } 

    public void Prj_Genrate_idAssgn(String table, String Field_Name) { 
     try { 
      String max = "select max(" + Field_Name + ") from" + table + ";"; 
      int max1 = Integer.parseInt(max); 
      System.out.println(max1); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

    public static void main(String[] args) { 
     dbtable2 dbtable2 = new dbtable2(); 
     dbtable2.Prj_Genrate_idAssgn(str, str1); 
    } 
} 

回答

0

您應該在查詢中添加一個空格,因爲您的手中必然會有SQLException。

"select max("+Field_Name+") from"+table+";"; 

"select max("+Field_Name+") from "+table+";"; 
+0

感謝您的代碼工作 – Ayesha 2011-12-22 13:04:01

+0

@Ayesha高興聽到。選擇一個答案來結束這個問題。 – Efthymis 2011-12-22 13:06:00

0

而不是使用聲明,您必須使用java.sql.PreparedStatement中。我已經編碼了您的Prj_Genrate_idAssgn函數。 首先聲明PreparedStatement statement;,其餘聲明部分與java.sql.ResultSet一起聲明爲ResultSet rs;,因爲這是您從查詢中獲取返回值的位置。希望這可以幫助你:

public void Prj_Genrate_idAssgn(String table,String Field_Name) 
{ 
    try 
    { 
    String max="select max("+ Field_Name + ") from " + table; 
    statement = conn.prepareStatement(max); 
    rs = statement.executeQuery(); 
    rs.next(); 
    int max1=Integer.parseInt(rs.getString(1)); 
    System.out.println(max1); 
    } 
    catch(Exception e) 
    { 
    System.out.println(e); 
    } 
} 

。希望可以解決查詢爲您服務。

問候

+0

謝謝你的朋友。代碼正在工作。 – Ayesha 2011-12-22 10:59:17

+0

@Ayesha:很高興知道。問候 – 2011-12-22 20:26:01

相關問題