如何将表和字段名称作为数据库查询参数传递?
在下面的代码中,我有一个名为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);
}
}
您应该在查询中添加一个空格,因为您的手中必然会有SQLException。
从
"select max("+Field_Name+") from"+table+";";
到
"select max("+Field_Name+") from "+table+";";
而不是使用声明,您必须使用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);
}
}
。希望可以解决查询为您服务。
问候
谢谢你的朋友。代码正在工作。 – Ayesha 2011-12-22 10:59:17
@Ayesha:很高兴知道。问候 – 2011-12-22 20:26:01
感谢您的代码工作 – Ayesha 2011-12-22 13:04:01
@Ayesha高兴听到。选择一个答案来结束这个问题。 – Efthymis 2011-12-22 13:06:00