Java JDBC Simple encapsulation method _java

Source: Internet
Author: User

Learn the JDBC after a period of time to write a simple package to try, so the reference to some of the information is not much good, after all, just learn not too long

First write the configuration file: Create a db.properties file directly under SRC and write the contents

<span style= "FONT-SIZE:18PX;" >mysqldriver=com.mysql.jdbc.driver 
mysqlurl=jdbc\:mysql\://localhost\:3306/one 
User=root 
Pwd= 123456 


Then write a 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; /** * This example is written with a small encapsulation * practice for JDBC Encapsulation * @author Hello * @version JDK 1.8/public class Readmain {static Properti 
      Es pos=null;//set static when loading classes only need one static{pos=new Properties ()//build peoperties to read config file try {//The following is used to read the configuration file 
    Pos.load (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("db.properties")); 
    catch (IOException e) {e.printstacktrace (); The public static Connection Getcon () {//establishes the Connection connection try {class.forname Pos.getproperty ("MysqlD 
      River "));//Load Com.mysql.jdbc.Driver}catch (classnotfoundexception e) {e.printstacktrace (); try {//load URL, User,password return 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 {//Shut down database connection an overloaded method is used to facilitate 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 closed (ResultSet rs,connection Co) {try {//close ResultSet Connection if (rs!=null) 
      Rs.close (); 
    if (co!=null) co.close (); 
    catch (Exception e) {e.printstacktrace (); 
    } public static void Closed (Connection co) {try {//close Connection if (co!=null) co.close (); 
    catch (Exception e) {e.printstacktrace (); 
 }}//End of 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 student (Id,name,sex,phone) VALUES (1235,?,?, 15896324131)"; Ps=con.preparestatement (SQL);//Get SQL statement//Here the The parameter is 1, the second is 2, ...//x the Paramete 
      R value//you can see that the subscript is a ps.setstring (1, name) starting from 1; 
      Insert the corresponding name into the datasheet ps.setstring (2, sex); 
      Insert the corresponding sex into the data table Ps.execute (); 
    Executes the SQL statement and has no return value System.out.println ("Insert succeeded"); 
    catch (Exception e) {e.printstacktrace (); } finally{Readmain.close (res, STA, cON); 

 Turn off Connection}} </span>

Here are two diagrams of steps to establish a db.properties


The above is the entire content of this article, I hope you can enjoy.

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.