Java implementation of database additions and deletions to the search

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.