Sunday, July 7, 2013

How To Connect MySQL Database Using Java

Hello

In this article, I'll show you how to connect MySQL database using java. For this, you should configure the settings about MySQL connector in the JAVA IDE first. So all things what we need are;


  • IDE (NetBeans or EClipse is so good!)
  • MySQL Connector

  • MySQL Connector is a jar file which is so small size. You can download MySQL Connector here and check this line up:
    JDBC Driver for MySQL (Connector/J)
    After downloading, you have to introduce IDE and Connector. I use NetBeans. Because of I will show you how to introduce on the NetBeans already. Actually It doesn't matter which program has used. For that reason This process is the same for EClipse.

    Screen views given above show us the process about introducing. You click OK and it's finish. Your MySQL driver and JAVA IDE know each other from on now. Let's create a db class on there.
    void vt() {
            Connection conn = null;
            String hostName = "jdbc:mysql://localhost:3306/";
            String dbName = "DatabaseName";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root";
            String password = "*****";
            try {
                conn = DriverManager.getConnection(
                    hostName+dbName,userName,password);
                System.out.println("Connected to the database");
                Statement st = conn.createStatement();
                ResultSet result = st.executeQuery("SELECT * FROM  test");
                while (result.next()) {
                   int id = result.getInt("id");
                   String name = result.getString("name");
                   System.out.println(id + "\t" + name);
            }
                conn.close();
            } catch (Exception e) {
                System.out.println("No Connection!");
            }
    }
    

    In the try..catch debugging part, if there is no connection, You are going to see "No Connection!" alert on the screen. If not, You'll get names of the test table from your database. This code given above is not so good, but useful. If you want to code better, you can use OOP for example. Create a class of database connection, and use it each time connection.
    See you next article!

    Friday, July 5, 2013

    CURL Requests With PHP

    Hello,
    Most developers prefer to use HTTP Request / Response service in their projects. In this situation, you have to send data as POST method to the opponent.

    Imagine that you are a web master of your own e-commerce web site. Members of the site use coupon during check out. In these conditions, you may connect to other systems, for example the store which is in another sector, and have to validate if the coupon is correct or something like that. Here is the magnificent specimen of pure one of the best example in the world for this article :)

    If you want, let's code CURL for now!

    For this, I set a service up for posting data: service.php
    if(isset($_POST['field'])) {
        print "Field is: ".$_POST['field'];
    }
    else {
        print "Field is blank!";    
    }
    
    Like you've just seen above, the service is waiting for field post variable on it. If you send a field data, the screen is going to be like,

    Field is your field value
    
    But else,

    Field is blank!
    
    I suppose to send a field value to the service: myfile.php

    //display error
    ini_set('display_errors', 1);
     
    //curl
    $ch = curl_init("http://localhost/CURL/service.php");
     
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'field=phpservisi.com');
     
    curl_exec($ch);
    curl_close($ch);
    
    If I run the myfile.php page, just going to see on on the screen like,

    Field is phpservisi.com
    
    For this example, I used a page which was http protocol. But sometimes I need use to https. In these conditions,have to add this line on it,

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    There are so many settings and options in CURL. If you want to check it out, you can visit PHP official page, here

    See you next articles!

    Thursday, July 4, 2013

    How To Generate EXCEL Files With PHP

    Hello,
    Today, I will share a very useful application with you guys, generating excel files with using PHP. As you know, we need reports as excel, pdf, word etc. in our web sites. For that reason, generating all of'em is important thing for us. Some of us use basic HTML files with using PHP & a data base. But this is not enough. Thus, I use generating excel.
    So, Let's code :)
    $filename = "myExcelFile_".date('Y-m-d-h-i').".xls"; 
    
    header("Content-Disposition: attachment; 
            filename=\"$filename\""); 
    
    header("Content-Type: application/vnd.ms-excel"); 
    
    header('Content-Type: application/x-msexcel; 
            charset=UTF-8; format=attachment;');
    
    echo "\tMy EXCEL DATA\r\n"; 
    
    exit;
    

    I set my file name up as myExcelFile_ plus current time and some header configuration. Then finally I print. That's it.
    If you run, you'll see this view:

    This article was short but so useful. You can develop better, of course.
    See you next article.

    Sunday, April 21, 2013

    R Package: mcga

    Machine coded genetic algorithm (MCGA) is a fast tool for real-valued optimization problems. It uses the byte representation of variables rather than real-values. It performs the classical crossover operations (uniform) on these byte representations. Mutation operator is also similar to classical mutation operator, which is to say, it changes a randomly selected byte value of a chromosome by +1 or -1 with probability 1/2. In MCGAs there is no need for encoding-decoding process and the classical operators are directly applicable on real-values. It is fast and can handle a wide range of a search space with high precision. Using a 256-unary alphabet is the main disadvantage of this algorithm but a moderate size population is convenient for many problems. Package also includes multi_mcga function for multi objective optimization problems. This function sorts the chromosomes using their ranks calculated from the non-dominated sorting algorithm.

    Package Url:

    http://cran.r-project.org/web/packages/mcga/index.html

    R Installation:

      install.packages ("mcga")
    

    For help and example type

      ?mcga
    
    
    in R console.


    Tuesday, October 16, 2012

    Encryption Functions in PHP: crypt() and md5()

    Hi! In this article, I am going to share encryption functions on PHP programming language with you. Well, there are several functions about this subject, but here the point is crypt and md5.


    md5 function is a text encryption. Here the text or string may be probably as password. md5 function makes the text a value which is 32-digit. Sure this value is probably going to be more complex than older text value.
    print md5("phpservisi.com");
    
    md5 function's output
    The example given above shows us phpservisi.com string value's output with md5 function.

    crypt function is the same mission with md5. Namely this is encryption function too. Here the variety is complexity of output. Because of it is some of us use this one, like me :) 

    One more feature is the output of crypt function's about making one-way string hashing. crypt function will return a hashed string using alternative algorithms that may be available on the system.

    Now, I'm coding about this:


    echo crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com"); 
    echo "\n".crypt("phpservisi.com");
    

    crypt function's output
    As you've seen on the top is crypt function's output. I did the same thing 8 times, and crypt function has just given us different results about those.

    If you want to learn more information about this subject, you can visit the PHP Manual web page: md5cryptsha1_filecrc32sha1hash