如何访问 Web 服务器 MySQL 数据库?
在您的项目中使用的 AccessRemoteMySQLDB-API 来访问 Web 服务器 MySQL 数据库。
引言
本文的目的是逐步展示如何使用 AccessRemoteMySQLDB
API 将 Java 与 Web 服务器 MySQL 连接起来。 在整篇文章中,我将创建关于 DML(Insert
、Update
、Select
、Delete
)的简单示例,以展示如何使用 Java 查询数据库。
为什么需要 AccessRemoteMySQLDB API?
例如,您正在开发一个应用程序(使用 Java),该应用程序将使用位于您的网站上、托管在 Linux 服务器上的 Web 服务器 MySQL 数据库,并支持 PHP 和 MySQL。 您可以使用以下讨论的方法来访问数据库。
方法 A
打开端口以访问 mysql
如果您的 Web 提供商允许您为给定的 IP 地址打开特定端口,这将是最佳解决方案,但我怀疑是否可行。 虽然您可以为所有端口打开,但这可能会引发安全问题,我强烈建议您不要这样做。
方法 B
使用 VPN 访问 mysql
另一种方法是在 Web 服务器和您的计算机之间安装某种 VPN。 VPN 设置完成后,您可以访问端口 3306,但此方法需要一些技能和经验,对于新的程序员和已开发应用程序的用户来说,它相当棘手。 可能会出现分发问题。 因此,此方法对程序员朋友来说也不是很有帮助。
正如我们所见,这两种方法都存在问题。 您可以为所有人打开端口,但会引发安全问题,而使用 VPN 连接对于用户来说很棘手。 因此,我推荐的最佳方法是在您的应用程序中使用 AccessRemoteMySQLDB
API。
入门
- 从 https://github.com/rohit7209/AccessRemoteMySQLDB 下载 API,并将压缩文件解压缩到您喜欢的目录
- 将文件 'handleSQL.php' 复制到您的
mysql
数据库所在的 Web 服务器
(请记住记下 'handleSQL.php' 的 URL。 URL 示例:example.com/somedirectory/handleSQL.php) - 将 AccessRemoteMySQLDB.jar 添加到您的项目中
- 就是这样,现在开始快乐编码吧 :)
Using the Code
以下是如何使用此 API 的简要演示。
将行插入 mysql 表格的示例代码
import armdb.ConnectHost; //import to make connection
import armdb.SQLQuery; //import to make SQL query
import armdb.SQLUpdate; //import to make SQL update
import armdb.QueryResult; //import to store the query results
import armdb.SQLQueryException; //import to handle the exception thrown during query
import armdb.SQLUpdateException; //import to handle the exception thrown during update
public class Example {
public static void main(String[] args) {
String fileURL="http://example.com/some_directory/handleSQL.php"; //URL of 'handleSQL.php' file
String host="mysql.some_hosting.com"; //server host name
String user="some_user"; //username
String pass="some_password"; //password
String DBName="some_dbname"; //database name
ConnectHost con=new ConnectHost(fileURL, host, user, pass, DBName); //make connection
SQLQuery query=new SQLUpdate(con); //create object to execute query statement
//insert rows in database table
try{
//sql statement
String sql="INSERT INTO table_name VALUES ('value_1','value_2','value_3','value_4')";
int rows=update.statement(sql); //execute statement and returns the no. of affected rows
System.out.println(rows+" no. of rows affected"); //prints no. of affected rows
}catch(SQLUpdateException e){ //catch exception if occurred
System.out.println(e.getMessage()); //print exception message
}
}
}
更新行的示例代码
//update contents of mysql table
try{
//statement
String sql="UPDATE table_name SET column_1='value_1' WHERE some_column='some_value')";
int rows=update.statement(sql); //executes statement
System.out.println(rows+" no. of rows affected"); //printing no. of affected rows
}catch(SQLUpdateException e){ //catch exception if occurred
System.out.println(e.getMessage()); //print exception message
}
删除行的示例代码
//delete contents of mysql table
try{
//statement
String sql="DELETE FROM table_name WHERE some_column='some_value')";
int rows=update.statement(sql); //executes statement
System.out.println(rows+" no. of rows affected"); //printing no. of affected rows
}catch(SQLUpdateException e){ //catch exception if occurred
System.out.println(e.getMessage()); //print exception message
}
选择行的示例代码
SQLQuery query=new SQLQuery(con); //SQLQuery object to execute select statement
QueryResult qr; //QueryResult object to store the queried result
try{
qr=query.statement("select * from table_name"); //execution of query statement
//qr holds the selected rows, let us print the values of some columns
//(say column_1 and column_2) of all rows
while(qr.nextFlag()){ //setting flag to next row till next row exists
//print column_1 & column_2 value of row where flag is set
System.out.println(qr.getValue("column_1")+", ");
System.out.print(qr.getValue("column_2"));
}
}catch(SQLQueryException e){ //catch exception if occurred
System.out.println(e.getMessage()); //print exception message
}
您还可以以其他方式获取结果
//print the contents of selected columns (say column_1 and column_2) and all rows using column index
while(qr.nextFlag()){ //setting flag to next row till next row exists
System.out.println(qr.getValue(0)+", "); //printing column_1 data of the row where flag is set
System.out.print(qr.getValue(1)); //printing column_2 data of the row where flag is set
}
//print the value of specific column and row
System.out.println(qr.getValueAt(0, 1));
//some more functions of QueryResult
//get no. of selected columns
System.out.println(qr.numFields());
//get no. of selected rows
System.out.println(qr.numRows());
//using nextFlag() and resetFlag()
qr.resetFlag(); //reset the flag to row 0
System.out.println(qr.getValue("column_1")); //prints the value of column_1 row 0
qr.nextFlag(); //increment the Flag by 1
System.out.println(qr.getValue("column_1")); //prints the value of column_1 row 1
qr.nextFlag(); //increment the flag by 1
qr.nextFlag(); //increment the flag by 1
System.out.println(qr.getValue("column_1")); //prints the value of column_1 and row 3
System.out.println(qr.getValue("column_1")); //again prints the same value as flag is not incremented
qr.resetFlag(); //reset the flag to row 0
System.out.println(qr.getValue("column_1")); //prints the value of column_1 and row 0 as flag
//is reset to 0
//clear the contents of qr
qr.clear();
更多建议
我们在建立连接附近硬编码了密码和其他详细信息,这不是一个好的做法。 我们应该隐藏这些详细信息,否则任何人都可以窥探这些详细信息。
String fileURL="http://example.com/some_directory/handleSQL.php"; //URL of 'handleSQL.php' file
String host="mysql.some_hosting.com"; //server host name
String user="some_user"; //username
String pass="some_password"; //password
String DBName="some_dbname"; //database name
ConnectHost con=new ConnectHost(fileURL, host, user, pass, DBName); //make connection
使用 EntityManager
-API 隐藏这些详细信息非常简单。 更多信息,请访问 此链接。