从mySQL数据库读取和写入

时间:2022-11-05 22:45:59

I want to create a java program which can connect to an online mySQL database. I have done some research and figured out that I should use an online mySQL database. In java, I understand there is a JDBC library for connecting to databases. How can I connect to the database online with reading and writing to it? I am looking into using the Google Cloud SQL. It says to connect to it like any other mySQL Database.

我想创建一个可以连接到在线mySQL数据库的java程序。我做了一些研究,发现我应该使用在线mySQL数据库。在java中,我知道有一个用于连接数据库的JDBC库。如何通过读写连接到数据库?我正在研究使用Google Cloud SQL。它说要像任何其他mySQL数据库一样连接到它。

Google Cloud SQL:Website

Google Cloud SQL:网站

Basically, my main question is: How can I connect to an online mySQL database with reading and writing to it from a java application?

基本上,我的主要问题是:如何通过从java应用程序读取和写入来连接到在线mySQL数据库?

Edit: Also is there any example for reading and writing to the database, not just how to connect?

编辑:还有任何读写数据库的例子,而不仅仅是如何连接?

2 个解决方案

#1


3  

You will need a connector, you can find at http://dev.mysql.com/downloads/connector/j/ Then you install, and next you should put in your build path, if you are using the eclipse, go right click on your project folder, properties, java build path libraries adn add external jar (the jar must be C:\Program Files \MySQL\MySQL Connector as mysql-connector-java-5.1.35-bin.jar ), next go order and export, select the jar imported and save.

您将需要一个连接器,您可以在http://dev.mysql.com/downloads/connector/j/找到然后安装,然后您应该放入您的构建路径,如果您正在使用eclipse,请右键单击在你的项目文件夹,属性,java构建路径库中添加外部jar(jar必须是C:\ Program Files \ MySQL \ MySQL Connector为mysql-connector-java-5.1.35-bin.jar),接下来去订购导出,选择导入并保存的jar。

After that you have installed the api to connect to the database for mysql

之后,您已经安装了api以连接到mysql的数据库

String mysql_jdb_driver = "com.mysql.jdbc.Driver";          // the connector for the database
String mysql_db_url = "jdbc:mysql://localhost/notas";       // url to connect, in this case is local but you can connect to an ip
String mysql_db_user = "root";                              // DB user name MYSQL
String mysql_pass = "";                                     //DB password

try{                                                                //try to connect to the DB
    Class.forName(mysql_jdb_driver).newInstance();
    conn = DriverManager.getConnection(mysql_db_url,mysql_db_user, mysql_pass );
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT ...");//put your select here
    stmt.executeUpdate("INSERT ...");//put insert, delete here

}
catch(Exception ex){                                
    ex.printStackTrace();
}
finally                                                              // always close the connecttion
{
    conn.close();           
}

#2


2  

as Roman suggested, all you need is in the link. If your application is not on the App Engine, just use include the mysql driver on the project and create the connection. Take a look at this: MySQL docs

正如罗马建议的那样,你只需要链接。如果您的应用程序不在App Engine上,只需在项目中使用包含mysql驱动程序并创建连接。看看这个:MySQL文档

#1


3  

You will need a connector, you can find at http://dev.mysql.com/downloads/connector/j/ Then you install, and next you should put in your build path, if you are using the eclipse, go right click on your project folder, properties, java build path libraries adn add external jar (the jar must be C:\Program Files \MySQL\MySQL Connector as mysql-connector-java-5.1.35-bin.jar ), next go order and export, select the jar imported and save.

您将需要一个连接器,您可以在http://dev.mysql.com/downloads/connector/j/找到然后安装,然后您应该放入您的构建路径,如果您正在使用eclipse,请右键单击在你的项目文件夹,属性,java构建路径库中添加外部jar(jar必须是C:\ Program Files \ MySQL \ MySQL Connector为mysql-connector-java-5.1.35-bin.jar),接下来去订购导出,选择导入并保存的jar。

After that you have installed the api to connect to the database for mysql

之后,您已经安装了api以连接到mysql的数据库

String mysql_jdb_driver = "com.mysql.jdbc.Driver";          // the connector for the database
String mysql_db_url = "jdbc:mysql://localhost/notas";       // url to connect, in this case is local but you can connect to an ip
String mysql_db_user = "root";                              // DB user name MYSQL
String mysql_pass = "";                                     //DB password

try{                                                                //try to connect to the DB
    Class.forName(mysql_jdb_driver).newInstance();
    conn = DriverManager.getConnection(mysql_db_url,mysql_db_user, mysql_pass );
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT ...");//put your select here
    stmt.executeUpdate("INSERT ...");//put insert, delete here

}
catch(Exception ex){                                
    ex.printStackTrace();
}
finally                                                              // always close the connecttion
{
    conn.close();           
}

#2


2  

as Roman suggested, all you need is in the link. If your application is not on the App Engine, just use include the mysql driver on the project and create the connection. Take a look at this: MySQL docs

正如罗马建议的那样,你只需要链接。如果您的应用程序不在App Engine上,只需在项目中使用包含mysql驱动程序并创建连接。看看这个:MySQL文档