Package Cn.hncu;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Java.util.Scanner;
Import Java.util.UUID;
Import Org.junit.Test;
Import Cn.hncu.pubs.ConnFactory;
public class Jdbcdemo {
Check
@Test
public void Readdemo () throws exception{
Class.forName ("Com.mysql.jdbc.Driver");//Driver---jdbc4.0 start, this sentence can be omitted.
Hibernate frame, don't save the last sentence.
String url = "Jdbc:mysql://127.0.0.1:3306/hncu?useunicode=true&characterencoding=utf-8";// Hncu This is where you're going to use your account login database 3306 is your database connection
mouth;
Connection con = drivermanager.getconnection (URL, "root", "1234");
System.out.println (con);
Statement st = Con.createstatement ();
ResultSet rs = st.executequery ("SELECT * from Person2"); This method is dedicated to querying
while (Rs.next ()) {
String id0 = rs.getstring (1);
Object id = rs.getobject (1);//Field ordinal
Object nm = rs.getobject ("name"); Field name
String ch = rs.getstring (3);
if (Ch.trim (). Equals ("0")) {
Ch= "female";
}else if (Ch.trim (). Equals ("1")) {
Ch= "Male";
}else{
Ch= "Unknown";
}
System.out.println (id0+ "-" +id+ "," +nm+ "," +ch ");
}
Con.close ();
}
Increase
@Test
public void Savedemo () throws exception{
Connection con = connfactory.getconn ();
Scanner sc = new Scanner (system.in);
String name = Sc.nextline ();
Increase
String uuid = Uuid.randomuuid (). toString (). Replace ("-", "");
String sql = "INSERT into Person2 VALUES ('" +uuid+ "', '" +name+ "', ' 1 ')";
INSERT into Person2 VALUES (' P09 ', ' Zhao Zilong ', ' 1 ');
Change
String sql = "UPDATE person2 SET name = CONCAT (name, '" +name+ "') WHERE LENGTH (ID) <4";
UPDATE person2 SET NAME = CONCAT (name, "x") WHERE LENGTH (ID) <4;
By deleting
String sex= "0";
String sql = "Delete from Person2 where sex= '" "+sex+" ";
Delete from Person2 where sex= ' 0 ';
SYSTEM.OUT.PRINTLN ("sql:" + SQL);
Statement st = Con.createstatement ();
int a = st.executeupdate (SQL); This method is generally used for the following operations: INSERT, delete, change update
Return value of the above method: number of rows affected
System.out.println ("Number of rows affected:" +a);
Con.close ();
}
}
Java implementation of database additions and deletions to the search