Java jdbc simple encapsulation and java jdbc simple Encapsulation
After learning jdbc for a while, I felt like I could try to write a simple encapsulation, so I wrote a little bit about some of the reference materials. After all, I didn't learn it too long.
First write the configuration file: directly create a db. properties file under src and then write the content
<span style="font-size:18px;">MysqlDriver=com.mysql.jdbc.DriverMysqlURL=jdbc\:mysql\://localhost\:3306/oneUser=rootPwd=123456</span>
Then write another class Code as follows:
<Span style = "font-size: 18px;"> package cn. java. ad; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. properties; /*** in this example, a small encapsulation * encapsulation exercise for jdbc * @ author hello * @ version jdk 1.8 */public class ReadMain {static Properties pos = null; // set static. Only one static {pos = new Propert is required during class loading. Ies (); // create Peoperties to read the configuration file try {// The following is the pos used to read the configuration file. load (Thread. currentThread (). getContextClassLoader (). getResourceAsStream ("db. properties ");} catch (IOException e) {e. printStackTrace () ;}} public static Connection getcon () {// establish a Connection try {Class. forName (pos. getProperty ("MysqlDriver"); // load com. mysql. jdbc. driver} catch (ClassNotFoundException e) {e. printStackTrace ();} try {// load URL, User, passwordh Turn DriverManager. getConnection (pos. getProperty ("MysqlURL"), pos. getProperty ("User"), pos. getProperty ("Pwd");} catch (SQLException e) {e. printStackTrace ();} return null;} public static void Close (ResultSet rs, Statement st, Connection co) {try {// close the database connection using the heavy load method to facilitate the encapsulation of if (rs! = Null) rs. close (); if (st! = Null) st. close (); if (co! = Null) co. close ();} catch (Exception e) {e. printStackTrace () ;}} public static void Close (ResultSet rs, Connection co) {try {// disable ResultSet Connectionif (rs! = Null) rs. close (); if (co! = Null) co. close ();} catch (Exception e) {e. printStackTrace () ;}} public static void Close (Connection co) {try {// Close Connectionif (co! = Null) co. close () ;}catch (Exception e) {e. printStackTrace () ;}}// end of the Program </span>
Then write the main Class Code as follows:
<Span style = "font-size: 18px;"> package cn. java. ad; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. statement; public class Main {public static void main (String [] args) {Connection con = null; ResultSet res = null; Statement sta = null; String SQL = null; string name = "Li Lei"; String sex = "male"; PreparedStatement ps = null; try {con = ReadMain. getcon (); SQL = "insert into s Tudent (id, name, sex, phone) VALUES (1235 ,?,?, 15896324131) "; ps = con. prepareStatement (SQL); // obtain the SQL statement // here the first parameter is 1, the second is 2 ,... // x the parameter value // you can see that the subscript starts from 1 ps. setString (1, name); // Insert the corresponding name into the data table ps. setString (2, sex); // Insert the corresponding sexinto the data table ps.exe cute (); // execute the SQL statement without returning the System. out. println ("inserted successfully");} catch (Exception e) {e. printStackTrace ();} finally {ReadMain. close (res, sta, con); // Close connections in turn }}</span>
The following figure shows the steps for creating db. properties.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.