Sunday 13 March 2016

Displaying and inserting data in NetBeans using Mysql Database

In the last session we learned about how to connect to mysql database using Netbeans.
Now we will learn how to display contents of database in a jTable and insert value in Database.

You need to download a jar file to display database values in jTable
https://docs.google.com/uc?authuser=0&id=0BzIr4IDDKJEcdDE1YTlzbmtkMzg&export=download

Creating Table

1.Start your mysql database then go to administration->open.
2.Expand your database and then click new.
3.Name the table basic and select the first column and add values  name - id, type - int, length - 255,      A_I - checked.
   In the second column set name - fname, type - varchar, length - 50.
   In the third column set name - lname, type - varchar, length - 50.
4.Click Save

Displaying contents in jTable

1.After downloading import the jar file into your project.Rclick on your project->libraries->add
   jar/file -> select your file ->click ok.
2.Insert a jTable in your frame and change the variable name to tab.
3.Go to the source tab and above the class name import
   import javax.swing.table.DefaultTableModel;
   import net.proteanit.sql.DbUtils;
4.Create a function named tab inside the class parenthesis.

 public void tab(){
        try{
            String sql = "select id,fname,lname from basic";
            pst = conn.prepareStatement(sql);
            rs=pst.executeQuery();
            tab.setModel(DbUtils.resultSetToTableModel(rs));
        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane,ex);
        }
    }
5.After init components add this line : conn = Data.ConnectDb(); and tab();


6.Run the file.

Inserting values in database

1.You need to insert three text field's.
2.Create a button and name it insert.

3.Double click the button and type the code : 

   String sql = "Insert into basic (id,fname,lname) values (?,?,?)";
        try{
            pst = conn.prepareStatement(sql);    
         pst.setString(1,jTextField1.getText());
         pst.setString(2,jTextField2.getText());
         pst.setString(3,jTextField3.getText());
        pst.execute();
        tab();
        JOptionPane.showMessageDialog(null,"Inserted Successfully!"); 
        }catch(Exception ex)
        {
           JOptionPane.showMessageDialog(null,ex); 
        }
4.You must make sure that the text field in which you are going to type your id is jTextField1, name
   is jTextField2 and last name is jTextField3.
5.Run the file and test.

Friday 11 March 2016

Connecting Mysql Database in NetBeans

Web hosting
Next -> http://dnetbeans.blogspot.in/p/in-last-session-we-learned-about-how-to.html

First for connecting mysql database in netbeans you need to do some things https://netbeans.org/kb/docs/ide/mysql.html Visit this site to know detailed information.

Alright,
Then for programming you need to download a jar file
http://www.java2s.com/Code/JarDownload/mysql/mysql-connector-java-5.1.23-bin.jar.zip
This is direct download link.

All set !!,

1.Then create a new java project.
2.Right click your project and go to properties->library->add jar/folder. Locate to your downloaded      jar file and then click open.
3.Create a java class in your project.Name the class Databaseconn
4.Type in the following code, eg. jdbc:mysql://localhost:integer/yourdatabasename


import java.sql.*;
import javax.swing.*;
public class Databaseconn {
Connection conn = null;
    static Connection ConnectDb() {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sahil","root","host");//Type your database host name here
            JOptionPane.showMessageDialog(null,"Connection Established");
            return conn;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
     
    }

}

*Note that i have not wrote the same name as given in my database.
The "root" and "" is my username and password.

5.Now create a jframe form and add a button, name it connect.
6.Open the code and type the following code

7.Now go to the design tab and then double click the button Or RightClick->Events->MouseClick
   Type the following code:
     conn = Databaseconn.ConnectDb();



8.Run the file and click connect.