Java database connection (with properties file) __ Database

Source: Internet
Author: User

This is their own free time summary of the connection to the database configuration file, I hope for beginners to learn help, if there are deficiencies, welcome you to give a lot of guidance. (This time, for example, to connect to the MySQL database)
Create a new suffix in your own project file directory. Properties of the configuration file, and then write the data inside:
The configuration file is written in the following figure, I use the MyEclipse2015 version, Maybe the screenshots will be a bit different from everyone else.

and then add the appropriate Mysql-connector-java-5.0.8-bin.jar package to run the code to connect successfully.

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import java.util.Properties;  public class Mysql_conn {private Connection conn;  Connection database private PreparedStatement prepar;   Used to receive preprocessing statements and handle them accordingly private ResultSet result;        Result set private Statement state;
    Used to process a static SQL statement and handle the corresponding private static String driver;
    private static String URL;
    private static String user;
    private static String password;
        Static/* The content inside the block will be read in advance before the program finds the main entrance to the function, so the password and username of the database and the driver will be ready in advance. {Properties Proper=new properties ();
            try {fileinputstream in =new fileinputstream ("src/jdbc_conn.properties");
            Proper.load (in);
            Driver=proper.getproperty ("Driver"); Url=proper.getproperty ("UrL ");
            User=proper.getproperty ("user");
        Password=proper.getproperty ("password");
        catch (IOException e) {e.printstacktrace ();
            } public Mysql_conn () throws ClassNotFoundException {try {class.forname (driver);
            Conn=drivermanager.getconnection (URL, user, password);
            State=conn.createstatement ();
        SYSTEM.OUT.PRINTLN ("Database connection succeeded");
        catch (SQLException e) {e.printstacktrace (); } public static void Main (string[] args) throws ClassNotFoundException {Mysql_conn mysqlconn=new
    Mysql_conn ();
 }

}

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.