After learning JDBC for a period of time feel oneself write a simple package to try, so reference to some of the information is not how good, after all, just learned not too long
First write the config file: Create a db.properties file directly under SRC and write the contents
<span style= "FONT-SIZE:18PX;" >mysqldriver=com.mysql.jdbc.drivermysqlurl=jdbc\:mysql\://localhost\:3306/oneuser=rootpwd=123456</span >
Then write a class code like this
<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 in a small package * for JDBC encapsulation exercises * @author Hello * @version JDK 1.8 */public class Readmain {static Properties pos=null;//set static on load Class is only required once static{pos=new Properties ();//build peoperties to read the configuration file try {//The following is the Pos.load (Thread.CurrentThread () that is used to read the configuration file. Getcontextclassloader (). getResourceAsStream ("db.properties")); catch (IOException e) {e.printstacktrace ();}} public static Connection Getcon () {//Establish Connection connection try {class.forname (Pos.getproperty ("Mysqldriver"));// Load Com.mysql.jdbc.driver}catch (classnotfoundexception e) {e.printstacktrace ();} try {//load URL, User,passwordreturn 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 database connection with overloaded method facilitates encapsulation 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 {//close 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.printstacktr Ace ();}}} 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) ";p s=con.preparestatement (SQL);//Get SQL statements//Here the first parameter is 1, the second is 2, ...//x the parameter Value //can see that the subscript is starting from 1 ps.setstring (1, name);//insert the corresponding name into the data table ps.setstring (2, sex);//insert the corresponding sex into the data table Ps.execute (); /Execute SQL statement and 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 that are steps to build db.properties
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JDBC Simple package for Java