使用jdbc连接数据库添加数据,相关删除 ,修改也可以使用此方法。
导入所需mysql-jdbc jar包
<!-- mysql-jdbc连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
package cn.cllover.administration.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; //逻辑生成虚拟数据 public class DataCreateTest { public static void main(String[] args) throws ClassNotFoundException, SQLException { //数据库字段编写 String stu_id; String stu_name = "jeans"; String stu_sex = "男"; int stu_age = 16; Long stu_phone = 16600259829L; String stu_home = "北京市海淀区"; String stu_address = "上海市浦东区"; String stu_birthday = "2017-12-31 23:59:59"; Long stu_identity = 0000000000000000L; String stu_nation = "汉族"; String stu_email = "18609933900@163.com"; Long stu_qq = 143699061L; String stu_league_menber = "是"; String stu_party_member = "否"; //数据库连接 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/administration-system?useUnicode=true&characterEncoding=UTF-8"; String user = "root"; String password = "password"; Connection connection = DriverManager.getConnection(url, user, password); if (!connection.isClosed()) { Statement statement = connection.createStatement(); for (int i = 1; i < 200; i++) { //sql设为自增 此操作可以不用填写 不自增下 可以使用递增编号 stu_id = "201901010111" + i; //sql语句 String sql = "INSERT INTO students(stu_name,stu_sex,stu_age,stu_phone,stu_home,stu_address,stu_birthday,stu_identity," + "stu_nation,stu_email,stu_qq,stu_league_member,stu_party_member) VALUES('"+stu_name+"'," + "'"+stu_sex+"','"+stu_age+"','"+stu_phone+"','"+stu_home+"','"+stu_address+"','"+stu_birthday+"','"+stu_identity+"','"+stu_nation+"'" + ",'"+stu_email+"','"+stu_qq+"','"+stu_league_menber+"','"+stu_party_member+"') "; // 输出(sql); statement.executeUpdate(sql); } //连接关闭 connection.close(); } } }
转载请注明出处。