Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Login form code

package advaced.java;

import com.mysql.jdbc.Statement;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

public class InsertDB extends javax.swing.JFrame {

Connection con=null;

Statement stm=null;

ResultSet rs=null;

public InsertDB() throws SQLException {

initComponents();

try {

con=DBconnection.getConnection();

System.out.println("connected");

} catch (Exception e) {

e.printStackTrace();

}
private void btnisertActionPerformed(java.awt.event.ActionEvent evt) {

String query ="INSERT INTO student(First_Name,Last_Name,Age)VALUES (?,?,?)";

try {

String name=txtfirstname.getText();

String lname=txtlastname.getText();

String age=txtage.getText();

PreparedStatement pst=con.prepareStatement(query);

pst.setString(1, name);

pst.setString(2, lname);

pst.setString(3,age);

int k=pst.executeUpdate();

if(k==1){

JOptionPane.showMessageDialog(this, "added recored");

txtfirstname.setText("");

txtlastname.setText("");

txtage.setText("");

txtfirstname.requestFocus();

// rs.close();

pst.close();

else

JOptionPane.showMessageDialog(null, "recored failed");

}
;

} catch (Exception e) {

System.err.println("System error");

public static void main(String args[]) throws SQLException {

InsertDB bb=new InsertDB();

bb.setVisible(true);

// Variables declaration - do not modify

private javax.swing.JButton btnisert;

private javax.swing.JButton btnupdate;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JTextField txtage;

private javax.swing.JTextField txtfirstname;

private javax.swing.JTextField txtlastname;


// End of variables declaration

DBconnectin code

package advaced.java;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DBconnection {

private final static String Username="root";

private final static String password="";

private final static String conn="jdbc:mysql://localhost/java";

public static Connection getConnection() throws SQLException{

return DriverManager.getConnection(conn, Username, password);

Public statatic void main------------

New InsertDB

You might also like