Saturday, 24 September 2016

mysql connectivity with Java in Swing

mysql connectivity with Java

create database,use,create table in mysql


here mysql username is root and password is vp
You need mysql connectivity .jar driver so Download it from mysql website
mysql-connector-java-3.1.14-bin.jar
Add this .jar file in projects properties by Right click and Java Build path
Add External jars
here add mysql connector java 3.1.14-bin.jar by file browsing in Eclipse
look like this

if u use Netbeans 
,do following 1.Open Netbeans IDE 2.Right-click your Project. 3.Select Properties. 4.On the left-hand side click Libraries. 5.Under "Compile" tab - click Add Jar/Folder button. 6.Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com) 7.Click OK Run Again... Its work.

This 2 lines are essential of mysql connectivity

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

                  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dblogin","root","vp");

here mysql username is root and password is vp and dblogin is the name of Database and 3306 is port.


how to find user in mysql
just write query in mysql
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+

1 row in set (0.41 sec)

How to find port in Mysql
just write select @port;
or
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+

1 row in set (0.00 sec)


set startup jframe1 or form1 as start in main method











Jframe frame = new Jframe();
frame.setVisible(true);


or
if you rename Jframe control as form1 then code will be as

 public static void main(String[] args) {
        // TODO code application logic here
        form1 frm=new form1();
        frm.setVisible(true);
    }

------------------------------------------------------------------------
here main will be like this
/*
 * Main.java
 *
 * Created on 
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cal;

/**
 *
 * @author Administrator
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        form1 frm=new form1();
        frm.setVisible(true);
    }
    

}

Thursday, 15 September 2016

swing addition of two num jtextField

addition of two numbers
using 3 jTextField

add this code in JButton click event

  int a=Integer.parseInt(jTextField1.getText());
  int b=Integer.parseInt(jTextField2.getText());
  int c=a+b;
  jTextField3.setText(String.valueOf(c));

for Float data type



to clear JtextField
write in ""
public class form1 extends javax.swing.JFrame {
 
    /** Creates new form form1 */
    public form1() {
       
        initComponents();
         jTextField1.setText("");
       jTextField2.setText("");
       jTextField3.setText("");
    }


note:   Integer float keyword in parse is from Java.lang



MessageBox in java



Right click..
Drag and drop JButton1 click event
To show messageBox

JOptionPane.showMessageDialog(null,"hello world");