MySQL additions and deletions check operation

Source: Internet
Author: User

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;ImportJava.util.Scanner;Importcom.mysql.jdbc.PreparedStatement; Public classSqloperate {//connect to the database and create a table     Public StaticConnection SqlConnect ()throwsException {String URL= "Jdbc:mysql://192.168.113.17:3306/test"; String User= "Base_hera"; String Password= "Eg6hme6pdc8cghz"; Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn=drivermanager.getconnection (URL, user, password); ResultSet result= Conn.getmetadata (). Gettables (NULL,NULL, "Student",NULL); if(Result.next ()) {System.out.println ("The table already exists"); } Else{Statement Statement=conn.createstatement (); String SQL= "CREATE table student (name char), age int (+), sex char (20))";            Statement.executeupdate (SQL); System.out.println ("CREATE TABLE succeeded"); }        returnConn; }    //New     Public Static voidAdd (Connection conn)throwsException {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter user name:"); String name=Scanner.nextline (); System.out.println ("Please enter the Gender:"); String Sex=Scanner.nextline (); System.out.println ("Please enter your Age:"); String Age=Scanner.nextline (); String SQL= "INSERT into student (Name,sex,age) VALUES (?,?,?)"; PreparedStatement PreparedStatement=NULL; PreparedStatement=(PreparedStatement) conn.preparestatement (SQL); Preparedstatement.setstring (1, name); Preparedstatement.setstring (2, Sex); Preparedstatement.setstring (3, age); inti =preparedstatement.executeupdate (); if(i = = 1) {System.out.println ("New success!" "); } Else{System.out.println ("New failed!" ");    } scanner.close (); }    //Enquiry     Public Static voidQuery (Connection conn)throwsException {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the user name you want to query:"); String name=Scanner.nextline (); String SQL= "SELECT * from student where name =?"; PreparedStatement PreparedStatement=NULL; PreparedStatement=(PreparedStatement) conn.preparestatement (SQL); Preparedstatement.setstring (1, name); ResultSet result=Preparedstatement.executequery (); //pointer moves to the first row of the result set        if(Result.next ()) {System.out.println ("Name:" + result.getstring (1) + "Age:" + result.getstring (2) + "Gender:" + result.getstring (3)); } Else{System.out.println ("Sorry, no such user, no query");    } scanner.close (); }    //Delete     Public Static voidDelete (Connection conn)throwsException {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the user name you want to delete:"); String name=Scanner.nextline (); String SQL= "Delete from student where name=?"; PreparedStatement PreparedStatement=NULL; PreparedStatement=(PreparedStatement) conn.preparestatement (SQL); Preparedstatement.setstring (1, name); inti =preparedstatement.executeupdate (); if(i = = 0) {System.out.println ("Sorry, no such user, cannot be deleted"); } Else{System.out.println ("Delete succeeded!" ");    } scanner.close (); }     Public Static voidMain (String args[])throwsException {Connection conn=SqlConnect (); System.out.println ("Please select:" + "\ n" + "1. Add user" + "\ n" + "2. Query user" + "\ n" + "3. Delete User"); Scanner Scanner=NewScanner (system.in); String s=Scanner.nextline (); if("1". Equals (s))        {Add (conn); } Else if("2". Equals (s))        {query (conn); } Else if("3". Equals (s))        {DELETE (conn);    } scanner.close (); }}

MySQL additions and deletions check operation

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.