JDBC Connectivity and GUI

JDBC Connectivity Using Java Application

Retrieving Data from Table

import java.sql.*;

public class JDBCConnection

{

public static void main(String[] args) {

try

{

//load the Driver

Class.forName("com.mysql.jdbc.Driver");

// Obtain a Connection

Connection con= DriverManager.getConnection("jdbc:mysql://172.16.100.8/rkmishradb","rkmishra", "a");

// Create Statement

Statement stmt=con.createStatement();

//Execute the Query and store the result in Resultset

ResultSet rs=stmt.executeQuery("select *from emp");

while(rs.next())

{

System.out.println(rs.getString(1) +" "+ rs.getString(2)+" "+rs.getString(3));

}

rs.close();

stmt.close();

con.close();

}

catch(Exception e)

{

System.out.println("Some Thing wrong in Connection");

}

}

}

Inserting Data to table using JForm and MySQL

Code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try

{

//Loading Driver

Class.forName("com.mysql.jdbc.Driver");

// Getting Mysql Connection

Connection con=DriverManager.getConnection("jdbc:mysql://172.16.100.8/20190999db","2019A7PS0999U","a");


// Creating Statement

Statement stmt=con.createStatement();

//stmt.executeUpdate("insert into IPL(PID,PNAME,PTEAM)values("+jTextField1.getText()+"+\","+jTextField2.getText()+""\"+","+"+jTextField2.getText()+"+")");

stmt.executeUpdate("insert into IPL (pid,pname,pteam) values ("+"\""+jTextField1.getText()+"\",\""+jTextField2.getText()+"\",\""+jTextField3.getText()+"\")");

JOptionPane.showMessageDialog(null, "Data Inserted Successfully");


stmt.close();

con.close();


}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "Something Wrong" + e);

}


Creating Project and Inserting values to Table

Click on New Project


Click on Java with Ant then Java Application and Create an application with name of "IPLDesktopApplication"


Design the Form using JFrame


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

// TODO add your handling code here:

}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try {

// TODO add your handling code here:

// Loading Driver

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://172.16.100.8/20190999db","2019A7PS0999U","a");

//Create a Statement

Statement stmt=con.createStatement();

// Executing Stataement

stmt.executeUpdate("insert into IPL(PID,PNAME,PTEAM)values("+"\""+jTextField1.getText()+"\""+","+"\""+jTextField2.getText()+"\""+","+"\""+jTextField2.getText()+"\""+")");

JOptionPane.showMessageDialog(null,"New Palyer Inserted Successfully");

stmt.close();

con.close();


}

catch (ClassNotFoundException ex) {

Logger.getLogger(NewPlayer.class.getName()).log(Level.SEVERE, null, ex);

} catch (SQLException ex) {

Logger.getLogger(NewPlayer.class.getName()).log(Level.SEVERE, null, ex);

}


}



private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try {

// TODO add your handling code here:

Class.forName("com.mysql.cj.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://172.16.100.8/20190048db","2019A7PS0999U","a");

//Create a Statement

Statement stmt=con.createStatement();

// Executing Stataement


String id=jTextField1.getText();

String name=jTextField2.getText();

String team = jTextField3.getText();

stmt.executeUpdate("insert into IPL(PID,PName,PTeam)values('"+id+"','"+name+"','"+team+"')");

JOptionPane.showMessageDialog(null, "NEW PLAYER INSERTED SUCCESSFULLY");

stmt.close();

con.close();

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, ex); }

}