This article describes the method of using jdbc to connect to MySQL database. Share it for your reference, as follows:
Use jdbc to connect to the database:
You can directly define url, user, psd and other information in the method, or you can read configuration files, but you must use the second method in a web project. In order to unify, only the second method is introduced.
step
1. Create configuration file db.properties
Whether it is eclipse or myeclipse, right-click ->new->file under the project, and use properties as the suffix.
Configuration file content:
#Connect the url of the database. If the host address is localhost and the port is 3306, it can also be written as url=jdbc:mysql:///databasenameurl=jdbc:mysql://localhost:3306/databasename#usernameuser=root#password password=root#MySQL database loading driverClass=com.mysql.jdbc.Driver
2. Define a tool class JdbcUtil.java that uses jdbc to connect to the database
Tool content:
public class JdbcUtil{ //Define global variable private static String url = null; private static String user = null; private static String password = null; private static driverClass = null; //Read the configuration file content and place it in a static code block, because only once it needs to be loaded static{ try{ Properties props = new Properties(); //Use classpath loading to read the configuration file//The read file path should start with "/", because if you use ".", the file cannot be found after deploying to the server. Using "/" will directly locate the project's src path InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties"); //Load the configuration file props.load(in); //Read the configuration file information url = props.getProperty("url"); user = props.getProperty("user"); password = props.getProperty("password"); driverClass = props.getProperty("driverClass"); //Register the driver Class.forName(driverClass); } catch(Exception e){ e.printStackTrace(); System.out.println("DriverRegister failed!!!"); } } //Get the connection object Connection public static Connection getConnection(){ try{ return DriverManager.getConnection(url,user,password); }catch(SQLException e){ e.printStackTrace(); //Run the runtime exception throw new RuntimeException(); } } //Close the connection method, and then close the public static void close(Connection conn,Statement stmt,ResultSet rs){ //Close the ResultSet object if(rs != null){ try{ //Close rs and set rs=null, because java will preferentially recycle variables with null value rs.close(); rs = null; }catch(SQLException e){ e.printStackTrace(); throw new RuntimeException(); } } //Close the Statement object, because PrepareStatement and CallableStatement are subinterfaces of Statement, so here only needs to have a method to close the Statement object if(stmt != null){ try{ stmt.close(); stmt = null; }catch(SQLException e){ e.printStackTrace(); throw new RuntimeException(); } } //Close the Connection object if(conn != null){ try{ conn.close(); conn = null; }catch(SQLException e){ e.printStackTrace(); throw new RuntimeException(); } } }}You can talk about any java issues, JavaSE, JavaEE
The tool class has been implemented and can be directly used in the project. However, one thing to note is that there is no supported class imported in this class file. You can also see that there is no package and import at the head of the class. This needs to be added manually. The shortcut key to import the class is Ctrl+Shift+O. Don't import the wrong package when importing the package; don't forget to introduce the MySQL support jar package mysql-connector-java-5.1.7-bin.jar
Attachment: mysql-connector-java-5.1.7-bin.jar can be downloaded by this site here.
For more information about Java-related content, please check out the topics of this site: "Java+MySQL database programming summary", "Java data structure and algorithm tutorial", "Java file and directory operation skills summary", "Java operation DOM node skills summary" and "Java cache operation skills summary"
I hope this article will be helpful to everyone's Java programming.