JAVA JDBC Simple additions and deletions change

Source: Internet
Author: User

 PackageJdbc_util;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJdbcdemo {//set Chinese character codingString Jdbcurl = "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8"; String ClassName= "Com.mysql.jdbc.Driver"; String User= "Root"; String Password= "920619";  PublicConnection getconnection () {Connection Connection=NULL; Try {            //Load Database driverClass.forName (className); //Get database connectionConnection =drivermanager.getconnection (jdbcurl, user, password); } Catch(Exception e) {System.out.println ("Connection Failed"); }        returnconnection; }     Public voidcloseconnection (Connection Connection) {Try{connection.close (); System.out.println ("Close Success"); } Catch(SQLException e) {System.out.println ("Shutdown failed"); }    }     Public voidInsert () {String SQL= "INSERT into user (Username,password,userage,usersex) VALUES (' C ', ' 123456 ', 20, 0)"; Connection Connection=getconnection (); Try{Statement Statement=connection.createstatement (); intresult =statement.executeupdate (SQL); if(Result! = 0) {System.out.println ("Operation succeeded, affected" + result + "line"); }        } Catch(SQLException e) {System.out.println ("Operation failed"); } finally{closeconnection (connection); }    }     Public voidDelete () {String SQL= "Delete from user-where UserId in (2,3,4)"; Connection Connection=getconnection (); Try{Statement Statement=connection.createstatement (); intresult =statement.executeupdate (SQL); if(Result! = 0) {System.out.println ("Operation succeeded, affected" + result + "line"); }        } Catch(SQLException e) {System.out.println ("Operation failed"); } finally{closeconnection (connection); }    }         Public voidUpdate () {String SQL= "Update user set UserName = ' B ' where UserId = 5"; Connection Connection=getconnection (); Try{Statement Statement=connection.createstatement (); intresult =statement.executeupdate (SQL); if(Result! = 0) {System.out.println ("Operation succeeded, affected" + result + "line"); }        } Catch(SQLException e) {System.out.println ("Operation failed"); } finally{closeconnection (connection); }    }         Public voidSelect () {String SQL= "SELECT * from User"; Connection Connection=getconnection (); Try{Statement Statement=connection.createstatement (); ResultSet ResultSet=statement.executequery (SQL);  while(Resultset.next ()) {System.out.println ("UserName =" + resultset.getstring ("UserName")); System.out.println ("PassWord =" + resultset.getstring ("PassWord")); System.out.println ("Userage =" + Resultset.getint ("Userage")); String Usersex= Resultset.getint ("usersex") = = 1? "Male": "Female"; System.out.println ("Usersex =" +usersex); }        } Catch(SQLException e) {System.out.println ("Operation failed"); } finally{closeconnection (connection); }    }    }

JAVA JDBC Simple additions and deletions change

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.