JDBC- deletions change operation check

Usage scenarios: the family needs to add bulk water test records into the family patriarch, but added manually add SQL statements and more trouble

Steps

Operating environment: Java8 + IDEA

1. Open IDEA

Click File-> New-> Project-> Package-> Java Class, file directory as follows:

Add-- increase Delete-- deleted

Find-- query Revise-- change

2. Download the Java connection MySQL driver

Download URL: http://www.manong5.com/104575704/

Download the package and good will before the files in the same directory can be

3. Write code

code show as below:

Add-- increase 

the Data Package; 

Import the java.sql *;.
Import java.util.Scanner;

public class the Add {
public static void main (String [] args) {
Scanner Scanner in = new new (the System.in);
System.out.println ( " enter ID: ");
int ID = in.nextInt ();
System.out.println (" Please enter the UID: ");
int UID = in.nextInt ();
System.out.println (" Please enter family_id: ");
int family_id in.nextInt = ();
System.out.println (" Please enter room_uid: ");
int room_uid in.nextInt = ();
System.out.println (" Please enter gift_record_id: ");
int = in.nextInt gift_record_id ();
System.out.println ( "Please enter charm_value:");
int charm_value = in.nextInt ();
System.out.println ( "Please enter the 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-- batch increase 
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 ( "Please enter the 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-- 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-- inquiry

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-- change

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, "F");
ps.setInt (. 3, 21 is);
ps.setString (. 4, "Hainan");
ps.setInt (. 5,. 7);
* /
ps.setInt (. 1, 161 004 535) ;
ps.setInt (2, 91 is);
ps.setInt (. 3, 62,480,728);
ps.setInt (. 4, 231 089);
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 ();
} the catch (SQLException E) {
System.out.println (e.getMessage ());
}
}
}

Note: Note that when using the host: port / dbname, password need to modify their connections to the database project

4. Use summary

Note that the data table when using the batch cycle to increase the uniqueness of the primary key data, and do not conflict with existing

Guess you like

Origin www.cnblogs.com/wanyuan/p/11217374.html