Java语言(最具噱头的语言)
Java给新人的印象应该是入门简单、代码优雅、活跃度高、跨平台、开源大家庭等等,实在是当之无愧的明星语言,而且是偶像派的。不过可惜的是,偶像派明星很容易被干掉。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);
}
}
}
}
以下是输出结果,表示程序是正确。
入门难度:★★★
代码优雅度:★★★★
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");
}
以下是程序的输出,代表代码是可正确。
入门难度:★★
代码优雅度:★★★
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");
}
以下是输出结果,代表程序可以正确。
入门难度:★★★★
代码优雅度:★★★
php语言(最低调奢华的语言)
PHP虽然近期也很火,但是总觉得它有点低调,但又不失内涵。作为网站制作最适合的语言之一,它总是默默的在发挥自己的力量。以下是PHP连接mysql低调的代码。
<?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);
?>
以下是程序运行结果,代表程序是正确。
入门难度:★★★
代码优雅度:★★★★
C#语言(最具潜力的语言)
以下是C#连接mysql数据库的代码。
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就难免让人联想到command模式,不知这API里面的实现是否是command设计模式。总的来说,C#和Java的mysql操作API还是差别比较大的。
入门难度:★★★
代码优雅度:★★★★
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()
以下是程序输出结果,代表程序的正确。
入门难度:★★★
代码优雅度:★★★★★
该文章在 2024/11/6 10:32:43 编辑过