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 ();
}
}