Monday, December 3, 2012

octal to decimal conversion Java

import java.util.Scanner;



public class OctalConversion {



    // simple main method for static Java little job

    public static void main(String[] arg) {

        // Scanner to read user input

       Scanner scan = new Scanner(System.in);

        // prompt for String

        System.out.print("Enter octal number: ");

        // read it back

        String octalStr = scan.nextLine();

        // convert to int

        int dec = convertToDecimal(octalStr);

        // if not -1 conversion was OK

        if(dec != -1)

            System.out.println("Octal: " + octalStr + " is in decimal " + dec);

    }

     

    static int convertToDecimal(String octo) {

        int number = 0;      // init result

        for(int i = 0; i < octo.length(); i++) { // pass through all input characters

            char digit = octo.charAt(i);            // fetch octal digit

            digit -= '0';                           // translate to number (integer)

            if(digit < 0 || digit > 7) {          // validate user inpu

                System.out.println("Your number is NOT a valid Octal number");

                return -1;

            }

            number *= 8;                            // shift to left what I already ahve

            number += digit;                        // add new number

        }

        return number;

    }

}

JDBC Get Row Count

The JDBC Get Row Count enables you to return the row count of a query. JDBC Get Row Count  provides you the name of the field, total number of row and data type of the column  to be retrieved from the table.
Understand with Example
In this Tutorial we want to describe a code that make you to understand in JDBC Get Row Count. The code help you to understand how to count the number of field and field name. The metadata in this code  provide you the property of the retrieved column. For this, Program include a class name JdbcGetRowCount, Inside this class we have a main method that include list of following steps -
 Import a package java.sql*,enables you to provide a network interface that help in communicating between the front end and the backend.
 Loading a driver is the next step to be followed by calling a class.forName( ),that accept driver class as argument.
The DriverManager.getConnection ( ) return you a connection object, the get Connection ( ) built a connection between url and database. The create Statement ( ) is used to create sql object. An object of connection is used to send and execute query in the database.
.execute query ( )  -   This is used to retrieve the record set from a table in the database and the record set from the table is stored in Result Set.   The select statement is used to retrieve the  record set from table.
 rs.get Metadata ( ) - This is used to return an object that can be used further to get information about the types and properties of the columns in a ResultSetMetaData object. The meta data call a getColumnCount ( ) return you the number of column in the table.
Finally the println print the Table Name, number of field and the field name.
Incase the exception occurred in the try block, subsequent the catch block check and handle the exception.
JdbcGetRowCount.java
import java.sql.*;

public class JdbcGetRowCount {

    public static void main(String args[]) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            st = con.createStatement();

            String sql = "select * from person";
            rs = st.executeQuery(sql);
            ResultSetMetaData metaData = rs.getMetaData();

            int rowCount = metaData.getColumnCount();

            System.out.println("Table Name : " + 
            metaData.getTableName(rowCount));
            System.out.println("No of Field : " + rowCount);
            System.out.print("Field Name :");
            for (int i = 0; i < rowCount; i++) {
                System.out.print("  " + metaData.getColumnLabel(i+1));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Output
Table Name : person
No of Field : 3
Field Name :  id  cname  dob

How to Convert Decimal to Binary Number in Java


Firstly, we have to draw control as Capture and set Variable for this control:










1/. Create a function name ReturnString:
    String ReturnString(String st)
    {
        String s="";
        char[] ch=st.toCharArray();
        for(int i=ch.length-1;i>=0;i--)
            s+=ch[i];
        return s;
    }
2/. In button Convert Number:

     private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        long value;
        value=Long.valueOf(txtDecimail.getText());
        String st="";
        do
        {
            st+=value%2;
            value/=2;
        }while(value>0);
        txtBinary.setText(ReturnString(st));
    }