以连接mysql为例:六种流行的语言比较

您所在的位置:网站首页 e4a数据库 以连接mysql为例:六种流行的语言比较

以连接mysql为例:六种流行的语言比较

2023-12-30 20:24| 来源: 网络整理| 查看: 265

本文是六种语言连接mysql数据库的代码展示,在LZ尝试的过程中,无论是语言环境搭建、mysql依赖库的导入还是代码的风格,各种语言都各有千秋。接下来,我们就让这些语言一一登场吧。

Java(最具噱头的语言)

Java给新人的印象应该是入门简单、代码优雅、活跃度高、跨平台、开源大家庭等等,实在是当之无愧的明星语言,而且是偶像派的。不过可惜的是,偶像派明星很容易被干掉。Java语言是LZ赖以生存的语言,因此LZ不希望做个偶像派,只能奋起直追,争取做实力派的Javaer。

说起这次Java连接mysql的编写,实在没什么好说的,毕竟本身就是做这个的,所以这一路非常顺利,算是最无感的一个。下面是LZ写的代码。

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); } } } }

输出结果如下: 在这里插入图片描述 总的来说,Java的mysql连接编写是最无感的一个,因为这是LZ比较熟悉的。就Java本身而言,LZ对它的打分如下。这里要特别说明的是,由于其它几种语言LZ都只是略知皮毛,因此本次只从最直观的两个感受去评分。

入门难度:★★★

代码优雅度:★★★★

C(最令人崇拜的语言)

这个语言在大学里LZ就学过一点皮毛,不过当时很厌烦这个无用的家伙,LZ想要写的是大型网游,而不是输出一个矩阵,或者打印个“心形”去泡妞。不过说来也怪,到得现在,LZ已经做了两年的程序猿,近半年的PM,却忽然之间对C这个老家伙情有独钟,敬意油然而生,实在是怪哉怪哉。

废话不多说,下面是LZ写的代码。

《c_mysql.h》 #ifndef C_MYSQL_H_ #define C_MYSQL_H_ #include #include #include #include #include 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 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

在这里插入图片描述 如果仔细观察会发现,PHP的API与C语言非常相似,这是因为PHP是使用DLL来扩展的mysql操作导致的。此外,PHP与前面三个不同的是,它不是编译型语言,是一种服务端的脚本语言,因此LZ选择使用命令来执行它。以下是LZ对它的评分。

入门难度:★★★

代码优雅度:★★★★

C#(最具潜力的语言)

C#最近虽然也算火爆,但却似乎一直有些力不从心的感觉,尽管LZ非C#成员,但也认识不少C#程序猿。不过LZ个人觉得,高级形态是语言的趋势,因此像C#、Java、Object-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还是差别比较大的,这让LZ有点出乎意料。以下是LZ对它的评分。

入门难度:★★★

代码优雅度:★★★★

python(最高端大气上档次的语言)

对于python来说,LZ之前就已经写过一篇小博文,无论从哪个角度来讲,这个语言都给LZ一种高大上的感觉。无论是它霸气的脚本语言特有的语法,还是特别的编码格式要求,都让LZ心生向往。

以下是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()

在这里插入图片描述 尽管python在安装mysql库的时候曾一度让LZ一筹莫展,但这依然无法阻止LZ对python的追逐之心。见到python的API可以说让LZ惊了个呆,实在是简洁至极,别具匠心。好了,多的就不说了,分数代表LZ的心。

入门难度:★★★

代码优雅度:★★★★★



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3