JAVA JDBC 连接Mysql 更新数据 双表查询更新

package com.test.json;

package com.test.javahome;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DataBaseTest {
    private static final String TABLE_01 = "dizhi2";
    private static final String TABLE_02 = "real_480";
    public static Connection getConnection() throws SQLException, java.lang.ClassNotFoundException {
        // 第一步:加载MySQL的JDBC的驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 取得连接的url,能访问MySQL数据库的用户名,密码;studentinfo:数据库名
        String url = "jdbc:mysql://localhost:3306/wumigen";
        String username = "root";
        String password = "qwert";

        // 第二步:创建与MySQL数据库的连接类的实例
        System.out.println("main ==============begin2");
        Connection con = DriverManager.getConnection(url, username, password);
        System.out.println("main ==============begin3");
        return con;
    }

    public static void main(String args[]) {
        try {
            System.out.println("main ==============begin");
            Connection con = getConnection();
            System.out.println("main ==============begin4");
            Statement sql_statement = con.createStatement();
            String query1 = "select * from "+TABLE_01;
            String query2 = "select * from "+TABLE_02;
            System.out.println("query1 ==============begin");
            ResultSet result = sql_statement.executeQuery(query1);
            ArrayList<px> pxlist = new ArrayList<px>();
            while (result.next()) {
                PX p1 = new PX();
                p1.type = result.getString(2);
                p1.number = result.getString(3);
                p1.img_320 = result.getString(4);
                pxlist.add(p1);
            }
            System.out.println("query1 ==============end, size = " + pxlist.size());
            System.out.println("query2 ==============begin");
            ResultSet result2 = sql_statement.executeQuery(query2);
            ArrayList<py> pylist = new ArrayList<py>();
            while (result2.next()) {
                PY p2 = new PY();
                p2.number = result2.getString(2);
                p2.img_480 = result2.getString(3);
                pylist.add(p2);
            }
            for (int i = 0; i < pxlist.size(); i++) {
                for (int j = 0; j < pylist.size(); j++) {
                    if (pylist.get(j).number == pxlist.get(i).number) {
                        pxlist.get(i).img_480 = pylist.get(j).img_480;
                    }
                }

                if (pxlist.get(i).img_320.length() == 0) {
                    System.out.println("=========null " + pxlist.get(i).number);
                } else {
                    String updateSql = "update "+TABLE_01+" set img_480='" + pxlist.get(i).img_480+
                            "' where number='"+ pxlist.get(i).number + "'";
                   System.out.println("begin to update ==> "+i);
                    sql_statement.execute(updateSql);
                }
            }
            System.out.println("query2 ==============end, size = " + pylist.size());
            sql_statement.close();
            con.close();
            System.out.println("=========end ");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException");
            System.err.println(e.getMessage());
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.err.println("SQLException: " + ex.getMessage());
        }
    }
}

class PX {
    String type;

    String number;

    String img_320;

    String img_480;
}

class PY {
    String number;

    String img_480;
}

</py></py></px></px>

你可能感兴趣的:(JAVA JDBC 连接Mysql 更新数据 双表查询更新)