Java JDBC Operations Database common code

Source: Internet
Author: User

1. Preparatory work

1 "

Create a new configuration file named Jdbc.properties to put it in SRC

2 "In the project to import the JDBC driver, pay attention to connect the different databases, the use of the driver is not the same, these can be found on the Internet

For more details on how to import jars, refer to http://blog.csdn.net/mazhaojuan/article/details/21403717

2. Code

1 ImportJava.io.InputStream;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4 ImportJava.sql.ResultSet;5 Importjava.sql.SQLException;6 Importjava.sql.Statement;7 Importjava.util.Properties;8 9  Public classMain {Ten      Public Static voidMain (string[] args) { OneDbutil Dbutil =Newdbutil (); ADBUTIL.R ("Select * FROM table"); -     } - } the  - classdbutil{ -     /** - * Get database connection +      * @return -      * @throwsException +      */ A      PublicConnection getconnection ()throwsexception{ at         //1. Create the configuration file and get the object input stream -InputStream is = This. GetClass (). getClassLoader (). getResourceAsStream ("Jdbc.properties.txt"); -         //2. Create Propetities -Properties JDBC =NewProperties (); - Jdbc.load (IS); -         //3. The corresponding value is obtained by means of Key-value inString Driver = jdbc.getproperty ("Driver"); -String url = jdbc.getproperty ("url"); toString user = Jdbc.getproperty ("User"); +String Password = jdbc.getproperty ("Password"); -         //4. Class objects when carrying a load the Class.forName (driver); *         //5 getting connected via DriverManager $Connection Connection =drivermanager.getconnection (Url,user,password);Panax Notoginseng         returnconnection; -          the     } +     /** A * Ways to release resources the      * @paramConnection +      * @paramStatement -      * @paramResultSet $      */ $      Public voidrelease (Connection connection,statement statement,resultset ResultSet) { -         Try { -             if(resultset!=NULL){ the resultset.close (); -             }Wuyi             if(statement!=NULL){ the statement.close (); -             } Wu             if(connection!=NULL){ - connection.close (); About             } $}Catch(SQLException e) { - e.printstacktrace (); -         } -          A     } +     /** the * Methods for querying the database -      * @paramSQL string, the SQL statement to execute if there are variables, use ' + variable + ' $      */ the      Public voidR (String sql) { theConnection Connection =NULL; theStatement Statement =NULL; theResultSet ResultSet =NULL; -         Try { inConnection =getconnection (); thestatement =connection.createstatement (); theResultSet =statement.executequery (SQL); About              while(Resultset.next ()! =false){ the                 //Here are some other things you can do theSystem.out.println (resultset.getstring (1)); the             } +}Catch(Exception e) { - e.printstacktrace (); the}finally {Bayi Release (connection, statement, ResultSet); the         } the     } -     /** - * How to change database records and additions the      * @paramSQL string, the SQL statement to execute if there are variables, use ' + variable + ' the      */ the      Public voidCUD (String sql) { theConnection Connection =NULL; -Statement Statement =NULL; theResultSet ResultSet =NULL; the         Try { theConnection =getconnection ();94statement =connection.createstatement (); theResultSet =statement.executequery (SQL); the              the             //This can be judged based on the return result, whether the statement executes successfully98 System.out.println (resultSet); About}Catch(Exception e) { - e.printstacktrace ();101}finally {102 Release (connection, statement, ResultSet);103         }104     } the     106}

Java JDBC Operations Database common code

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.