JDBC Operation database Add, delete, update, find example Analysis _java

Source: Internet
Author: User

The examples in this article describe the methods of adding, deleting, updating, and finding JDBC operations databases. Share to everyone for your reference, specific as follows:

Package Cn.com.JDBC;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement; public class CRUD {public static void main (string[] args) throws SQLException {//TODO auto-generated the method stub/
  /create ();
  Update ();
  Delete ();
 Read ();
  The static void Delete () throws SQLException {Connection conn=null;
  Statement St=null;
  ResultSet Resultset=null;
   try {//2. Establish a connection conn=jdbcutils.getconnection ();
   Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection ();
   3. Create statement st=conn.createstatement ();
   4. Execution Statement String sql= "Delete from user where id>5";
   int i=st.executeupdate (SQL);
  System.out.println ("i=" +i);
  finally {Jdbcutils.free (resultset, ST, conn);
  } static void Update () throws SQLException {Connection conn=null;
  Statement St=null;
  ResultSet Resultset=null;
   try {//2. Establish a connection conn=jdbcutils.getconnection (); Single Case design pattern conn=jdbcutilssingle.getinstance (). GetConnection ();
   3. Create statement st=conn.createstatement ();
   4. Execute statement String sql= "Update user set money=money+20";
   int i=st.executeupdate (SQL);
  System.out.println ("i=" +i);
  finally {Jdbcutils.free (resultset, ST, conn);
  } static void Create () throws SQLException {Connection conn=null;
  Statement St=null;
  ResultSet Resultset=null;
   try {//2. Establish a connection conn=jdbcutils.getconnection ();
   Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection ();
   3. Create statement st=conn.createstatement ();
   4. Execute statement String sql= "insert into User (Name,birthday,money) VALUES (' WY ', ' 2011-09-23 ', ' 2894656 ')";
   int i=st.executeupdate (SQL);
  System.out.println ("i=" +i);
  finally {Jdbcutils.free (resultset, ST, conn);
  } static void Read () throws SQLException {Connection conn=null;
  Statement St=null;
  ResultSet Resultset=null;
   try {//2. Establish a connection conn=jdbcutils.getconnection ();
   Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection (); 3. CreateStatement st=conn.createstatement ();
   4. Execution Statement Resultset=st.executequery ("Select Id,name,birthday,money from user");
    5. Processing results while (Resultset.next ()) {System.out.println (Resultset.getobject ("id"));
    System.out.println (Resultset.getobject ("name"));
    System.out.println (Resultset.getobject ("Birthday"));
   System.out.println (Resultset.getobject ("money"));
  finally {Jdbcutils.free (resultset, ST, conn);
}} package Cn.com.JDBC;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
 public class Jdbcutils {private static String url= "JDBC:MYSQL://LOCALHOST:3306/JDBC";
 private static String user= "root";
 private static String password= "123";
  Private Jdbcutils () {} static {try {Class.forName ("com.mysql.jdbc.Driver");
  catch (ClassNotFoundException e) {throw new Exceptionininitializererror (e); }} public static Connection getconnection () throws SQLException {return drivermanager.getconnection (URL, user, password);
   public static void Free (ResultSet resultset,statement st,connection conn) {//6. Releasing resources try{if (resultset!=null)
  Resultset.close ();
  catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
   Finally {try {if (st!=null) st.close ();
   catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
     finally {if (conn!=null) try {conn.close ();
     catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

 }
   }
  }
 }
}

I hope this article will help you with your Java programming.

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.