Java語言(最具噱頭的語言)
Java給新人的印象應該是入門簡單、代碼優(yōu)雅、活躍度高、跨平臺、開源大家庭等等,實在是當之無愧的明星語言,而且是偶像派的。不過可惜的是,偶像派明星很容易被干掉。Java語言是LZ賴以生存的語言,因此LZ不希望做個偶像派,只能奮起直追,爭取做實力派的Javaer。
說起這次Java連接mysql的編寫,實在沒什么好說的,畢竟本身就是做這個的,所以這一路非常順利,算是最無感的一個。代碼如下:
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);
}
}
}
}
以下是輸出結果,表示程序是正確。
入門難度:★★★
代碼優(yōu)雅度:★★★★
C語言(最令人崇拜的語言)
《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");
}
以下是程序的輸出,代表代碼是可正確。
入門難度:★★
代碼優(yōu)雅度:★★★
C++語言(最神秘莫測的語言)
《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");
}
以下是輸出結果,代表程序可以正確。
入門難度:★★★★
代碼優(yōu)雅度:★★★
php語言(最低調(diào)奢華的語言)
PHP雖然近期也很火,但是總覺得它有點低調(diào),但又不失內(nèi)涵。作為網(wǎng)站制作最適合的語言之一,它總是默默的在發(fā)揮自己的力量。以下是PHP連接mysql低調(diào)的代碼。
<?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);
?>
以下是程序運行結果,代表程序是正確。
入門難度:★★★
代碼優(yōu)雅度:★★★★
C#語言(最具潛力的語言)
以下是C#連接mysql數(shù)據(jù)庫的代碼。
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 connection = new MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='123456';charset='utf8';pooling=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();
}
}
}
}
以下是程序運行結果,代表著程序是可以正確。
C#的API有些特別,而且看到有command就難免讓人聯(lián)想到command模式,不知這API里面的實現(xiàn)是否是command設計模式??偟膩碚f,C#和Java的mysql操作API還是差別比較大的。
入門難度:★★★
代碼優(yōu)雅度:★★★★
python語言(最高端大氣上檔次的語言)
以下是python高端大氣上檔次的代碼。
# coding=utf-8
import MySQLdb
import 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()
以下是程序輸出結果,代表程序的正確。
入門難度:★★★
代碼優(yōu)雅度:★★★★★
該文章在 2024/11/6 10:32:43 編輯過