Six popular language meals---c, c + +, Python, Java, PHP, C # Which one do you prefer?

Source: Internet
Author: User
Tags mysql code

Introduction

In view of the 51 period between the super-large flow of people, LZ reasoning, ultimately choose to dwelling in their own rental housing. Boredom, of course, can not forget to do something, so LZ on the study of a few languages of the fur, here caught dead a turn, I hope you ape friend Mo to laughed at.

However, it is also a coincidence that the garden in the previous time just staged a language war, which makes the LZ Boven born may be involved in unnecessary disputes. In fact, LZ has long wanted to write such an article, but the language of inquiry is more troublesome, because each language is often the most difficult to do is the environment of the building, and very waste of time, so the LZ picked 51 this opportunity to begin to proceed.

Anyway, LZ still want to clarify, this article is only LZ learning results show, resolutely not participate in the language war. If the ape friends have to ask the LZ on the language, LZ can only give you a LZ read fantasy novel.

This novel is about a magician and samurai world, when each person was born, the body will have "Kanagi Fire and Earth Thunder Wind" one of the characteristics of some of the attributes, some days the wizard will have a variety of properties. We are all from the first layer to the nine floor, the 10 layer becomes God. In this system of cultivation, there is a special but very reasonable setting, that is, before becoming God, we all will draw the corresponding attribute elements from the air to cultivate, in order to understand the heaven. However, after realizing that heaven became God, the attributes of the man of God would vanish, and even the samurai and the sorcerer would be no different. For those who become gods, they control only the Supreme divine power. No attribute, no occupation.

Well, the story is here, do not know the ape friends understand it?

Language dinner

Return to the point, this article is six languages to connect MySQL database code display, in the LZ attempt process, whether it is the language environment, MySQL dependency library import or code style, all languages are different. Next, let's put these languages on the scene.

Java (most gimmick language)

  

Java to the new impression should be easy to get started, code elegance, high activity, cross-platform, open source family and so on, is a well-deserved star language, and is an idol faction. But it's a pity that Idol stars are easy to kill. The Java language is the LZ depends on the survival of the language, so LZ do not want to do an idol faction, can only catch up and strive to do the Javaer.

Speaking of this Java connection MySQL writing, there is really nothing to say, after all, it is to do this, so the road is very smooth, is the most non-sense of one. The following is the LZ write code.

Package Cn.zxl.jmysql;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.ResultSet;import    Java.sql.statement;public class Jmysql {private static final String DRIVER = "Com.mysql.jdbc.Driver";    private static final String URL = "Jdbc:mysql://localhost/test";    private static final String USERNAME = "root";    private static final String PASSWORD = "123456";        private static final String SQL = "SELECT * from Test";        public static void Main (string[] args) {Connection Connection = null;        Statement Statement = null;        ResultSet ResultSet = null;            try {class.forname (DRIVER);            Connection = Drivermanager.getconnection (URL, USERNAME, PASSWORD);            statement = Connection.createstatement ();            ResultSet = Statement.executequery (SQL); while (Resultset.next ()) {System.out.println ("|" + resultset.getstring ("id") + "|" + resultset.getstring ("            Name ") +" | ");  }      } catch (Exception e) {System.out.println ("Query failed!");                } finally {try {resultset.close ();                Statement.close ();            Connection.close ();            } catch (Exception e) {throw new RuntimeException (e); }        }    }    }

The following is the output, which indicates that the program is correct.

In general, Java MySQL Connection writing is the most non-sense of one, because this is the LZ more familiar. As far as Java itself is concerned, the LZ scores it as follows. In particular, since the other languages LZ is only superficial knowledge, so this time only from the most intuitive two experience to score.

  Entry Difficulty: ★

Code Elegance: ★★★★

  

C (Most admired language)

  

The language in the university LZ learned a little fur, but was tired of this useless guy, LZ want to write a large online games, rather than output a matrix, or print a "heart" to the girls. But it is also strange, to now, LZ has done two years of the procedure ape, nearly half a year pm, but suddenly to c this old guy love has a single clock, respect arises spontaneously, it is strange that is strange zai.

Not much nonsense to say, below is the LZ write code.

"C_mysql.h" #ifndef c_mysql_h_#define c_mysql_h_#include <stdio.h> #include <stdlib.h> #include < winsock2.h> #include <windows.h> #include <mysql.h>void execute_sql (char* sql); #endif
"C_mysql.c" #include "c_mysql.h" #define HOST "localhost" #define USERNAME "root" #define PASSWORD "123456" #define    DATABASE "Test" int main () {char *sql = "SELECT * from Test";    Execute_sql (SQL); return 0;}    void Execute_sql (char* sql) {MYSQL connection;    Mysql_res *result_pointer;    Mysql_row Result_row;    int result, row, column, I, J;    Mysql_init (&connection);        if (NULL = = Mysql_real_connect (&connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, client_found_rows)) {        printf ("Error:connection failed!\n");    Return    } mysql_query (&connection, "Set names GBK");    result = mysql_query (&connection, SQL);        if (result) {printf ("Error:query failed!\n");        Mysql_close (&connection);    Return    } Result_pointer = Mysql_store_result (&connection);        if (result_pointer) {row = Mysql_num_rows (Result_pointer); for (i = 1; i < row + 1; i++) {Result_row = Mysql_fetch_row (rEsult_pointer);        printf ("|%s|%s|\n", Result_row[0], result_row[1]);    }} mysql_close (&connection); System ("Pause");}

The following is the output of the program, which indicates that the code is functioning correctly.

Perhaps because Java cannot manipulate pointers, so when writing C, use &, *, and these symbols feel particularly cool, do not know whether the real CER has such a feeling. The following is the LZ's C language rating.

  Entry Difficulty: ★

Code Elegance: ★

  

C + + (the most mysterious language)

  

C + + as the most difficult language, called the mystery of the test is not too much, can use C + +, is very few. C + + has this impression, mainly because the LZ look at the Java Virtual Machine source of the tragic experience, that full screen macro definition, really make LZ for their IQ catch urgency, but also be written Java Virtual machine of the great God deeply impressed.

The following is the highest-depth language connection to MySQL code, please see.

"C++_mysql.h" #ifndef c___mysql_h_#define c___mysql_h_#include <iostream> #include <mysql_connection.h>   #include <mysql_driver.h>   #include <statement.h>using namespace sql;   Using namespace Std;void execute_sql (const SQLString SQL); #endif
 c++_mysql.cpp #include "c++_mysql.h" #define HOST "localhost" #define USERNAME "root" #define PASSWORD "123456" #    Define DATABASE "Test" int main () {const SQLString sql = "SELECT * from Test";    Execute_sql (SQL); return 0;}       void execute_sql (const SQLString SQL) {Mysql::mysql_driver *driver;       Connection *connection;       Statement *statement;       ResultSet *result_set;       Driver = Mysql::get_mysql_driver_instance ();       Connection = Driver->connect ("tcp://localhost:3306", "root", "123456");       statement = Connection->createstatement ();      Statement->execute ("Use test");    Statement->execute ("Set names GBK");       Result_set = statement->executequery (sql); while (Result_set->next ()) {cout << "|" << result_set->getint ("id") << "|" << ;       Result_set->getstring ("name") << "|" << Endl;       } DELETE statement;      Delete connection; System ("Pause");} 

The following is the output, which indicates that the program can run correctly.

The MySQL connection in C + +, with its API similar to Java, is roughly estimated to be about 90%. So the C + + program in addition to exception catching and memory cleanup and Java Some differences, the other code is very similar, but LZ always feel that the use of "." No use of "-" appears high-end, 0.0. The following is the LZ rating.

  Entry Difficulty: ★★★★

Code Elegance: ★

PHP (lowest-luxury language)

Although PHP is also very hot recently, but always feel it a little low-key, but without losing its connotation. As one of the most suitable languages for website production, it always silently plays its own power.

The following is a low-key PHP connection to MySQL code.

<?php    $mysql _server_name= "localhost";    $mysql _username= "root";    $mysql _password= "123456";    $mysql _database= "Test";        $connection = mysql_connect ($mysql _server_name, $mysql _username, $mysql _password);    if (! $connection) {        echo "connection failed!";        return;    }    Mysql_set_charset ("GBK", $connection);    mysql_select_db ($mysql _database, $connection);    $sql = "SELECT * from Test";    $result =mysql_query ($sql, $connection);    while ($row = Mysql_fetch_array ($result)) {        echo "|". $row ["id"]. "|". $row ["Name"]. "| \ n ";    }    Mysql_close ($connection);? >

The following is the result of the program operation, the representative program is correct.

If you look closely, the PHP API is very similar to the C language because PHP is the result of MySQL operations that use DLLs to extend. In addition, PHP is different from the previous three, it is not a compiled language, is a server-side scripting language, so the LZ chooses to use the command to execute it. Below is the LZ rating for it.

  Entry Difficulty: ★

Code Elegance: ★★★★

C # (most potential language)

  

C # has recently been a hot, but it seems to have some sense of inadequacy, although LZ non-C # members, but also know a lot of C # program ape. But LZ personally think, advanced form is the trend of language, so like C #, Java, object-c This is highly encapsulated language will always glow fever, after all, after a few decades to go back, using the assembly even binary to write code of the day must not come again.

The following is the code for the C # connection to the MySQL database.

Using system;using system.collections.generic;using system.linq;using system.text;using MySql.Data.MySqlClient; Namespace csmysql{class Program {static void Main (string[] args) {mysqlconnection Connec tion = new Mysqlconnection ("database= ' Test ';D ata source= ' localhost '; User id= ' root ';            password= ' 123456 '; charset= ' UTF8 ';p ooling=true ');            Mysqlcommand command = new Mysqlcommand (); Command.            Connection = Connection;            Command.commandtext = "SELECT * from Test"; try {command.                Connection.Open (); Mysqldatareader reader = command.                ExecuteReader (); while (reader. Read ()) {Console.WriteLine ("|" + Reader. GetInt32 ("id") + "|" + Reader.                GetString ("name") + "|");            } console.readline ();            } catch (Exception) {Console.WriteLine ("Query failed!"); } finally {command.            Connection.close (); }        }    }}

The following is the result of the program operation, which means that the program can be run correctly.

C # API is a bit special, and see command is inevitably reminiscent of the command mode, I do not know whether the implementation of this API is the command design mode. In general, C # and Java MySQL Operation API is still very different, which makes LZ a bit unexpected. Below is the LZ rating for it.

  Entry Difficulty: ★

Code Elegance: ★★★★

Python (highest-level language in the atmosphere)

  

For Python, the LZ has written a small post before, regardless of from which point of view, the language gives the LZ a tall feeling. Whether it is its domineering scripting language-specific syntax, or special coding format requirements, all let the LZ heart yearning.

Below is the code for Python's high-end atmospheric grade.

# coding=utf-8import mysqldbimport sys host = ' localhost ' user = ' root ' password  = ' 123456 '   db   = ' Test '  if __name__ = = ' __main__ ':    connection = MySQLdb.connect (host,user,password,db);    Try:        connection.ping ()    except:        print (' failed to connect MySQL. ')    sql = ' SELECT * FROM Test '    cursor = connection.cursor ()    cursor.execute (SQL) for    row in cursor:        print ("|" + str (row[0]) + "|" + row[1] + "|")    Cursor.close ()    connection.close ()    sys.exit ()

The following is the program output results, representing the correctness of the program.

Although Python helpless the LZ during the installation of the MySQL library, it still does not prevent the LZ from chasing the python. See the Python API can be said to let LZ surprised a stay, it is very concise, originality. Well, many will not say, the score represents the LZ's heart.

  Entry Difficulty: ★

Code Elegance: ★★★★★

Summary

  

This is the end of a simple display of six languages. For this small tour of the language, LZ prefers python to this land, concise syntax and API directly conquered the LZ. I do not know if you have any of the ape friends to which the heartbeat?

/\/\\  / \/

Six popular language meals---c, c + +, Python, Java, PHP, C # Which one do you prefer?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.