如何将表和字段名称作为数据库查询参数传递?

时间:2011-12-22 08:07:38

标签: java mysql swing

在下面的代码中,我有一个名为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);
    }
}

2 个答案:

答案 0 :(得分:0)

您应该在查询中添加一个空格,因为您必须掌握SQLException。

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

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

答案 1 :(得分:0)

您必须使用java.sql.PreparedStatement,而不是使用Statement。我再次编写了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);
   }
} 

希望可以为您解决问题。

此致