Add, delete, modify, and query JDBC-add, delete, update, and search instances for Databases

Source: Internet
Author: User

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 method stub // create (); // update (); delete (); read () ;}static void delete () throws SQLException {Connection conn = null; Statement st = null; ResultSet resultset = null; try {// 2. establish connection con N = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4. execute the statement String SQL = "delete from user where id> 5"; int imo-st.exe cuteUpdate (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 connection conn = Jdbc Utils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4. execute the statement String SQL = "update user set money = money + 20"; int imo-st.exe cuteUpdate (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 connection conn = JdbcUti Ls. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4. execute the statement String SQL = "insert into user (name, birthday, money) values ('wy', '2017-09-23 ', '123 ')"; int iw.st.exe cuteUpdate (SQL); System. out. println ("I =" + I);} finally {JdbcUtils. free (resultset, st, conn) ;}} static void read () throws SQLException {Connection conn = null; Statement st = null; ResultSet re Sultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4.execute the sentence resultset=st.exe cuteQuery ("select id, name, birthday, money from user"); // 5. processing result while (resultset. next () {System. out. println (resultset. getObject ("id"); System. out. println (resultset. getObject ("name"); System. out. println (resultset. getObject ("birthda Y "); 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 Stri Ng 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. release resource try {if (resultset! = Null) resultset. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {try {if (st! = Null) st. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (conn! = Null) try {conn. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}}

Related Article

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.