Requirement: Based on MYSQL, implement the last Zheng code query java program implemented with SQLite and improve it: input aacm, then output "no comment". When a Chinese character or a Chinese character word is input, the Zheng code code of that character or word is output.
1. Set up Zheng code table in MySQL database
Use Navicat Premium, a graphical management tool, to connect to MySQL database, and create a data table named "zmb" under the database "test".
2. code
import java.sql.*;
import java.util.Scanner;
public class ZMQ {
public static void main(String[] args) {
String databaseName = "test";
String url = "jdbc:mysql://localhost:3306/" + databaseName + "?useSSL=false" ;//Database name
String user = "root";//MySQL user name
String password = "123456";//MySQL password
while(true) {
System.out.print("Please enter:");
Scanner scan=new Scanner(System.in); //Waiting for keyboard input
String x =scan.next(); //Read the input characters and put them in x
try {
Class.forName("com.mysql.jdbc.Driver");//load driver
Connection con = DriverManager.getConnection(url, user, password);//configure connections
Statement stat = (Statement) con.createStatement();//Create statement class object to execute SQL statement
String sql = "select * from zmb"; //Query statement
ResultSet rs = stat.executeQuery(sql); //Store obtained results
boolean b =false;//Define whether the flag is used to find the flag
while(rs.next()){
String zm=rs.getString("zm");
String hz=rs.getString("hz");
if(zm.equals(x)) {
System.out.println("Corresponding Chinese characters:"+hz);//Output Chinese characters corresponding to Zheng code
b =true;
}
if(hz.equals(x)) {
System.out.println("Corresponding code:"+zm);//Output Zheng code corresponding to Chinese characters
b =true;
}
}
if("ByeBye".equals(x)) {
System.exit(0);//End procedure
}
if(!b) {
System.out.println("No corresponding word found");
}
rs.close();
con.close();
} catch(ClassNotFoundException e) { //Handling exceptions
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
3. Compilation and operation results
The command line runs the java program. When introducing the jar package, you can use the following methods:
java -cp .;A.jar B
A is your jar package. Just put it in the same folder as B.
The results of compiling and running are as follows: