My first java code to link mysql

Before we start, we need mysql database and some data

Using a virtual machine, a server2008 system is virtualized. Installed mysql and created a database.
I can use navicat software to visually edit the database, which is very convenient.
Write picture description here
On the server with IP 192.168.0.11, I created a new database named gmbussiness, and then created a table named goods_price in it. There are not many contents in the table.
Write picture description here
Click Design Table to create a table structure.
Then just enter some information at will.
Write picture description here

java code part

Double-click to open the MyEclipse Professional 2014 software, and create a new project.
Create a new file called MySQLDemo.java. Then enter the following code

package game;
import java.sql.*;
public class MySQLDemo {
    public static void main(String[] args) {
    //以上都是固定用法,死记硬背



        //声明一个数据库链接,起名conn,这个地方酌情改代码
        Connection conn;
        //声明一个数据库驱动地址,不知何用,固定用法,不写就报错。这个地方酌情改代码
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
        //声明mysql数据库的网络IP+端口+链接的数据库名字,这个地方酌情改代码
        String DB_URL = "jdbc:mysql://192.168.0.11:3306/gmbusiness";
        //声明连接数据库的用户名,这个地方酌情改代码
        String user = "root";
        //声明连接数据库的密码,这个地方酌情改代码
        String password = "root";



        //然后开始连接,没有写注释的部分可以认为是固定用法, 
        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,user,password);
            Statement Statement = conn.createStatement();
            String Connection;


            //最关键的操作数据库语句,"SELECT id, price FROM goods_price"
            //意思是:搜索id,price从表格goods_price中。这个地方酌情改代码

            String sql = "SELECT id, price FROM goods_price";

            //固定用法
            ResultSet rs = Statement.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索,这个地方酌情改代码
                int jid  = rs.getInt("id");
                float jprice = rs.getFloat("price");

                // 输出数据,这个地方酌情改代码
                System.out.print("商品ID: " + jid);
                System.out.print(", 出售单价: " + jprice);
                System.out.print("\n");
            }

            // 完成后关闭数据库链接,固定用法
            rs.close();
            conn.close();

            //如果出现意外连接不上数据库,或数据库检索代码错误,则用下面方法处理。下面是固定用法,不必深究。
        }catch(ClassNotFoundException e) {   
                        //数据库驱动类异常处理
                     System.out.println("Sorry,can`t find the Driver!");   
                        e.printStackTrace();   
                        } catch(SQLException e) {
                     //数据库连接失败异常处理
                        e.printStackTrace();  
                        }catch (Exception e) {
                        e.printStackTrace();
                 }finally{
                     System.out.println("数据库数据成功获取!!");
                 }
    }
}

You can try to run it, but if you run it now, it will definitely report an error, because you have not configured the mysql driver. Remember the mysql driver URL in the code just now?

Mysql driver configuration method:

Go to Baidu and download a mysql-connector-java-5.1.27.jar file, and then create a new folder called lib
in the project , and throw this mysql-connector-java-5.1.27.jar file into it, as shown below.

Write picture description here

Then, right-click the project name, game, build path, configure build path in Chinese is called configuration project path

Write picture description here

Then click Libraries in the pop-up window and click the Add JARs... button on the right ,

Then select the mysql-connector-java-5.1.27.jar file just now in the pop-up new window, and click OK below.

Write picture description here

At this time, you will see one more mysql-connector-java-5.1.27.jar in the list.

Write picture description here

OK, click to run, and try your java.
If you are successful, you will get the following output in the console

商品ID: 1, 出售单价: 10.0
商品ID: 2, 出售单价: 9.0
商品ID: 3, 出售单价: 56.587
商品ID: 4, 出售单价: 1510.51
商品ID: 5, 出售单价: 50.516
商品ID: 6, 出售单价: 561.3
数据库数据成功获取!!

Thanks to netizen Fengxueyeguirenshen for his wonderful article:
Reference: http://www.cnblogs.com/centor/p/6142775.html

Guess you like

Origin blog.csdn.net/leelizc/article/details/74607710