Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I pass the table and field names as database query parameters?
    primarykey
    data
    text
    <p>In the code below, I have a method named <code>Prj_Genrate_idAssgn</code> with formal parameters <code>String table, String Field_Name</code>. How can I can pass the database table name (<code>studentrecords</code>) and field name (<code>Reg_no</code>) as a parameters (<code>String table, String Field_name</code>)?</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload