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)); } } }
运行查看结果: