使用场景:测试家族族长分成时需要批量添加家族流水记录,但手动添加和SQL语句添加较为麻烦
操作步骤
运行环境:Java8+IDEA
1.打开IDEA
点击File—>New—>Project—>Package—>Java Class,文件目录如下:
Add--增加 Delete--删除
Find--查询 Revise--更改
2.下载Java连接MySQL驱动
下载网址:http://www.manong5.com/104575704/
下载好将包和之前的文件放在同一目录即可
3.编写代码
代码如下:
Add--增加
package Data;
import java.sql.*;
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入id:");
int id = in.nextInt();
System.out.println("请输入uid:");
int uid= in.nextInt();
System.out.println("请输入family_id:");
int family_id = in.nextInt();
System.out.println("请输入room_uid:");
int room_uid = in.nextInt();
System.out.println("请输入gift_record_id:");
int gift_record_id = in.nextInt();
System.out.println("请输入charm_value:");
int charm_value = in.nextInt();
System.out.println("请输入type:");
int type = in.nextInt();
System.out.println("请输入create_time:");
String create_time = in.next();
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
//建立连接
try {
Connection con;
String url = "jdbc:mysql://host:port/dbname";
String user = "root";
String password = "password";
con = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO family_charm_record (id,uid,family_id,room_uid,gift_record_id,charm_value,type,create_time) values(?,?,?,?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.setInt(2, uid);
ps.setInt(3, family_id);
ps.setInt(4, room_uid);
ps.setInt(5, gift_record_id);
ps.setInt(6, charm_value);
ps.setInt(7, type);
ps.setString(8, create_time);
int num = ps.executeUpdate();
System.out.println(num);
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
BatchAdd--批量增加
package Data;
import java.sql.*;
//import java.util.Scanner;
public class BatchAdd {
public static void main(String[] args) {
/*
Scanner in = new Scanner(System.in);
System.out.println("请输入id:");
int id = in.nextInt();
System.out.println("请输入uid:");
int uid = in.nextInt();
System.out.println("请输入family_id:");
int family_id = in.nextInt();
System.out.println("请输入room_uid:");
int room_uid = in.nextInt();
System.out.println("请输入gift_record_id:");
int gift_record_id = in.nextInt();
System.out.println("请输入charm_value:");
int charm_value = in.nextInt();
System.out.println("请输入type:");
int type = in.nextInt();
System.out.println("请输入create_time:");
String create_time = in.next();
*/
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
//建立连接
try {
Connection con;
String url = "jdbc:mysql://host:port/dbname";
String user = "root";
String password = "password";
con = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO family_charm_record (id,uid,family_id,room_uid,gift_record_id,charm_value,type,create_time) values(?,?,?,?,?,?,?,?)";
//PreparedStatement ps = con.prepareStatement(sql);
int i = 1;
while (i < 5) {
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 983 + i);
ps.setInt(2, 161004534);
ps.setInt(3, 91);
ps.setInt(4, 62480729);
ps.setInt(5, 230121);
ps.setInt(6, 1000);
ps.setInt(7, 2);
ps.setString(8, "2019-07-19 00:00:00");
i++;
int num = ps.executeUpdate();
System.out.println(num);
ps.close();
}
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Delete--删除
package Data;
import java.sql.*;
public class Delete {
public static void main(String[] args) {
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
String url = "jdbc:mysql://host:port/dbname";
String user = "root";
String password = "password";
try {
Connection con = DriverManager.getConnection(url, user, password);
String sql = "delete from family_charm_record where id in (985,986)";
PreparedStatement ps = con.prepareStatement(sql);
int num = ps.executeUpdate();
System.out.println(num);
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Find--查询
package Data;
import java.sql.*;
public class Find {
public static void main(String[] args) {
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
String url = "jdbc:mysql://host:port/dbname";
String user = "root";
String password = "password";
Connection con = DriverManager.getConnection(url, user, password);
String sql = "SELECT a.phone,n.noble_name FROM account a,noble_users n WHERE a.uid=n.uid;";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
/*
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
String address = rs.getString(5);
System.out.printf("%4s %4s %4s %4s %4s\n",id,name,sex,age,address);
*/
String phone=rs.getString(1);
String noble_name=rs.getString(2);
System.out.printf("%4s %4s\n",phone,noble_name);
}
rs.close();
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Revise--更改
public class Revise {
public static void main(String[] args) {
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
String url = "jdbc:mysql://host:port/dbname";
String user = "root";
String password = "password";
try {
Connection con = DriverManager.getConnection(url, user, password);
String sql = "update family_charm_record set uid=?,family_id=?,room_uid=?,gift_record_id=?,charm_value=?,type=?,create_time=? where id=984";
PreparedStatement ps = con.prepareStatement(sql);
/*
ps.setString(1, "小林子");
ps.setString(2, "女");
ps.setInt(3, 21);
ps.setString(4, "海南");
ps.setInt(5, 7);
*/
ps.setInt(1, 161004535);
ps.setInt(2, 91);
ps.setInt(3, 62480728);
ps.setInt(4, 231089);
ps.setInt(5, 2000);
ps.setInt(6, 2);
ps.setString(7, "2019-07-19 17:00:30");
int num = ps.executeUpdate();
System.out.println(num);
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
注:使用时注意将host:port/dbname,password需要修改为自己项目的数据库连接
4.使用总结
使用循环批量增加数据时需要注意数据表的主键唯一性,不要和已有的发生冲突