java實現mysql數據庫從一張表插入數據到另一張表


 

創建兩張表:

create table employee(
    id varchar(18),
    name varchar(18),
    email varchar(100),
    gender varchar(10)
);


create table copyEmployee(
    id varchar(18),
    name varchar(18),
    email varchar(100),
    gender varchar(10)
);

插入數據:

insert into employee values("1","Alice","Alice@163.com","femail");
insert into employee values("2","yaooo","yaooo@163.com","mail");

 

domain:

package cn.gtmc.schedule.domain;

/**
 * Created by Yaooo on 2019/8/12.
 */
public class Employee {
    private String id;
    private String lastName;
    private String email;
    private String gender;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

 

 

package cn.gtmc.schedule.app;


import cn.gtmc.schedule.domain.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yaooo on 2019/8/12.
 */
public class ScheduleApp {

    public Connection getCon() {
        //數據庫連接名稱
        String username="root";
        //數據庫連接密碼
        String password="root";
        String driver="com.mysql.jdbc.Driver";
        //其中test為數據庫名稱
        String url="jdbc:mysql://node1:3306/test";
        Connection conn=null;
        try{
            Class.forName(driver);
            conn=(Connection) DriverManager.getConnection(url,username,password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    public void addData(Employee employee){
        String sql="insert into copyEmployee (id, name, email, gender) values(?, ?, ?, ?)";

        java.sql.PreparedStatement ptmt = null;
        try {
            ptmt = getCon().prepareStatement(sql);
            ptmt.setString(1, employee.getId());
            ptmt.setString(2, employee.getLastName());
            ptmt.setString(3, employee.getEmail());
            ptmt.setString(4, employee.getGender());
            ptmt.execute();//執行給定的SQL語句,該語句可能返回多個結果
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Employee> getSelect() {
        String sql = "select * from employee";
        Connection conn = getCon();
        PreparedStatement pst = null;
        // 定義一個list用於接受數據庫查詢到的內容
        List<Employee> list = new ArrayList<Employee>();
        try {
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                Employee employee = new Employee();
                // 將查詢出的內容添加到list中
                String id = rs.getString("id");
                String lastName = rs.getString("name");
                String email = rs.getString("email");
                String gender = rs.getString("gender");
                employee.setLastName(lastName.substring(2,4));      //這邊做字段處理
                employee.setId(id);
                employee.setEmail(email);
                employee.setGender(gender);
                addData(employee);
            }
        } catch (Exception e) {
        }
        return list;
    }

    public static void main(String[] args){
        ScheduleApp app = new ScheduleApp();
        List<Employee> list = app.getSelect();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

 

運行查看結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM