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.