This package is only responsible for implementing database operations.
Do not take other actions
Package Com.zhidisoft.dao;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.zhidisoft.entity.Dog;
Used to implement database operation connection, release database general additions and deletions to search
public class Databaseaction {
Dog dog = new Dog ();
Connection conn = null;
Connecting to a database Zhang San
Public Connection getconnection () {
String url = "Jdbc:mysql://localhost:3306/test?"
+ "User=root&password=root&useunicode=true&characterencoding=utf8";
try {
Class.forName ("Com.mysql.jdbc.Driver");
conn = drivermanager.getconnection (URL);
} catch (ClassNotFoundException | SQLException e) {
E.printstacktrace ();
}//dynamic load MySQL Driver
Return conn;
}
//inserting information into the database
public int zhuce (dog dog) {
conn = This.getconnection ();
String sql = "INSERT into dogs () VALUES (" + dog.getnumber () + ", '" + dog.getname () + "', '" + dog.getsex ()
+ "'," + dog.getage () + ", '" + dog.getstrain () + "', '" + dog.getbingqingzhuangtai () + "')";
Statement St;
int result = 0;
try {
st = Conn.createstatement ();
result = st.executeupdate (sql);
} catch (SQLException e) {
e.printstacktrace ();
}
return result;
}
public int Delete (int number) {
conn = This.getconnection ();
String sql = "Delete from dogs where number=" + number + "";
Statement St;
int result = 0;
try {
st = Conn.createstatement ();
result = St.executeupdate (SQL);
SYSTEM.OUT.PRINTLN (result);
} catch (SQLException e) {
E.printstacktrace ();
}
return result;
}
public int Genggai (string name, String Bingqingzhuangtai) {
conn = This.getconnection ();
String sql = "Update dogs set bingqingzhuangtai= '" + Bingqingzhuangtai + "' where name= '" + name + "'";
Statement St;
int result = 0;
try {
st = Conn.createstatement ();
result = St.executeupdate (SQL);
SYSTEM.OUT.PRINTLN (result);
} catch (SQLException e) {
E.printstacktrace ();
}
return result;
}
Public void Chaxun (String bingqingzhuangtai) throws SQLException {
conn = This.getconnection ();
String sql = "SELECT * from dogs where bingqingzhuangtai= '" + Bingqingzhuangtai + "'";
Statement St;
ResultSet result;
st = Conn.createstatement ();
result = st.executequery (sql);
//SYSTEM.OUT.PRINTLN (result);
While (Result.next ()) {
System.out.println (Result.getint ("number") + "\ T" + result.getstring ("name") + "\ T"
+ result.getstring ("sex") + "\ T" + result.getint ("age") + "\ T" + result.getstring ("strain") + "\ T"
+ result.getstring ("Bingqingzhuangtai"));
}
}
public void Chaxun2 (String name) throws SQLException {
conn = This.getconnection ();
string sql = "SELECT * from dogs where bingqingzhuangtai= '" + name + "'";
statement St;
resultset result;
st = Conn.createstatement ();
result = St.executequery (sql);
while (Result.next ()) {
System.out.println (Result.getint ("number") + "\ T" + result.getstring ("name") + "\ T"
+ result.getstring ("sex") + "\ T" + result.getint ("age") + "\ T" + result.getstring ("strain") + "\ T"
+ result.getstring ("Bingqingzhuangtai"));
}
}
This package is the package that holds the properties of the data.
For example, the properties of this animal dogs class
Package com.zhidisoft.entity;
public class Dog {
private int number;
private String name;
Private String sex;
private int age;
Private String strain;
Private String Bingqingzhuangtai;
Public String Getbingqingzhuangtai () {
return Bingqingzhuangtai;
}
public int GetNumber () {
return number;
}
public void Setnumber (int number) {
This.number = number;
}
public void Setbingqingzhuangtai (String bingqingzhuangtai) {
This.bingqingzhuangtai = Bingqingzhuangtai;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getstrain () {
return strain;
}
public void Setstrain (String strain) {
This.strain = strain;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
}
There is also a package that is designed to handle the business logic of the package, which does not participate in other matters
Just dealing with business logic
Package Com.zhidisoft.UI;
Import java.sql.SQLException;
Import Java.util.Scanner;
Import com.zhidisoft.dao.DatabaseAction;
Import Com.zhidisoft.entity.Dog;
Public class Test {
Public static void Main (string[] args) throws SQLException {
databaseaction dz=new databaseaction ();
Dog Dog=new Dog ();
//Focus on business logic
System.out.println ("Welcome to the Pet Hospital, please select the operation: 1. Registration 2. Delete 3. Change information 4. Inquiries based on case Information 5. Search for information according to pet name");
Scanner input=new Scanner (system.in);
String Choice=input.next ();
if (choice.equals ("1")) {
//Register Operation
//Collect Data Call database Insert method to implement registration
System.out.println ("Enter pet number:");
Dog.setnumber (Integer.parseint (Input.next ()));
System.out.println ("Enter the name of the pet:");
Dog.setname (Input.next ());
System.out.println ("Enter the sex of the pet:");
Dog.setsex (Input.next ());
System.out.println ("Enter the age of the pet:");
dog.setage (Integer.parseint (Input.next ()));
System.out.println ("Enter pet variety:");
Dog.setstrain (Input.next ());
System.out.println ("Pet Symptoms:");
Dog.setbingqingzhuangtai (Input.next ());
//Call the DAO layer code to implement data insertion
dz.zhuce (dog);
}
if (Choice.equals ("2")) {
System.out.println ("Enter the pet number that will be deleted:");
Dog.setnumber (Integer.parseint (Input.next ()));
Dz.delete (Dog.getnumber ());
}
if (Choice.equals ("3")) {
System.out.println ("Enter the name of the pet that will be changed:");
Dog.setname (Input.next ());
System.out.println ("Please enter the pathological state to be changed");
Dog.setbingqingzhuangtai ((Input.next ()));
Dz.genggai (Dog.getname (), Dog.getbingqingzhuangtai ());
}
if (Choice.equals ("4")) {
System.out.println ("Please enter the pathological state to be queried");
Dog.setbingqingzhuangtai ((Input.next ()));
Dz.chaxun (Dog.getbingqingzhuangtai ());
}
if (Choice.equals ("5")) {
System.out.println ("Please enter the name of the pet to be queried");
Dog.setname ((Input.next ()));
Dz.chaxun2 (Dog.getname ());
}
John Doe calling Zhang San's code
Connection conn=dz.getconnection ();
}
}
This is just a preliminary step-by-step process, which facilitates the efficient completion of the project
Java JDBC Connection Number library Simple tiering operation