Skip to main content

Database programming

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
mozum
Offline
Joined: 2012-05-01

Can any one help me on how to insert data into database in netbeans IDE using JTextField and clicking a submith button. I generated the following code as a java project and successfully connected a database. But i could not populate the database through the form i designed.

package mozum2;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Mozum extends JFrame
implements ActionListener
{
JLabel lab1 = new JLabel("S.NO");
JTextField f1 = new JTextField(10);
JLabel lab2 = new JLabel("Title");
JTextField f2 = new JTextField(10);
JLabel lab3 = new JLabel("Author");
JTextField f3 = new JTextField(10);
JButton sub = new JButton("Submit");
JButton ex = new JButton("Exit");
public static void main(String[] args)
{
Mozum mz = new Mozum();
try{
String host = "jdbc:mysql://localhost:3306/test";
String uName = "admin";
String uPass = "";
Connection con = DriverManager.getConnection(host,uName,uPass);
Statement stmt = con.createStatement();
String SQl = "SELECT * FROM counselor";
ResultSet rs = stmt.executeQuery(SQl);
while(rs.next())
{
int counselor_id = rs.getInt("COUNSELOR_ID");
String first_name = rs.getString("FIRST_NAME");
String nick_name = rs.getString("NICK_NAME");
String last_name = rs.getString("LAST_NAME");
String p = counselor_id + " " + first_name + " " + nick_name + " " + last_name;
System.out.println(p);

}
}
catch(SQLException err){
System.out.println(err.getMessage());
}
}
public Mozum()
{
setLayout(new FlowLayout());
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.PINK);
JPanel pn = new JPanel();
pn.add(lab1);
pn.add(f1);
add("West", pn);

JPanel pn1 = new JPanel();
pn1.add(lab2);
pn1.add(f2);
add("West", pn1);

JPanel pn2 = new JPanel();
pn2.add(lab3);
pn2.add(f3);
add("West", pn2);

JPanel pn3 = new JPanel();
pn3.add(sub);
pn3.add(ex);
add("West", pn3);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == sub)
try{
String host = "jdbc:mysql://localhost:3306/test";
String uName = "admin";
String uPass = "";
Connection con = DriverManager.getConnection(host,uName,uPass);
Statement stmt = con.createStatement();
String first_name = f1.getText();
String nick_name = f2.getText();
String last_name = f3.getText();
String sql="Insert into counselor values('"+(first_name)+"','"
+(nick_name)+"','"+(last_name)+"')";
stmt.executeUpdate(sql);
}
catch(SQLException err){
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dan_codrean_ro
Offline
Joined: 2012-05-21

Your code looks very bad :(.

I have changed it a little bit just to show you, where to call the update function.

class Mozum extends JFrame {

JLabel lab1 = new JLabel("S.NO");
JTextField f1 = new JTextField(10);
JLabel lab2 = new JLabel("Title");
JTextField f2 = new JTextField(10);
JLabel lab3 = new JLabel("Author");
JTextField f3 = new JTextField(10);
JButton sub = new JButton("Submit");
JButton ex = new JButton("Exit");

public static void main(String[] args) {
Mozum m = new Mozum();
try {
String host = "jdbc:mysql://localhost:3306/test";
String uName = "admin";
String uPass = "";
Connection con = DriverManager.getConnection(host, uName, uPass);
Statement stmt = con.createStatement();
String SQl = "SELECT * FROM counselor";
ResultSet rs = stmt.executeQuery(SQl);
while (rs.next()) {
int counselor_id = rs.getInt("COUNSELOR_ID");
String first_name = rs.getString("FIRST_NAME");
String nick_name = rs.getString("NICK_NAME");
String last_name = rs.getString("LAST_NAME");
String p = counselor_id + " " + first_name + " " + nick_name + " " + last_name;
System.out.println(p);

}
} catch (SQLException err) {
System.out.println(err.getMessage());
}
}

public Mozum() {
setLayout(new FlowLayout());
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.PINK);
JPanel pn = new JPanel();
pn.add(lab1);
pn.add(f1);
add("West", pn);

JPanel pn1 = new JPanel();
pn1.add(lab2);
pn1.add(f2);
add("West", pn1);

JPanel pn2 = new JPanel();
pn2.add(lab3);
pn2.add(f3);
add("West", pn2);

JPanel pn3 = new JPanel();
pn3.add(sub);
pn3.add(ex);
add("West", pn3);
setVisible(true);

sub.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("call here update");
}
});

ex.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
System.exit(1);
}
});
}
}