After studying JDBC for a while, I felt that I wrote a simple package to try it, so I wrote some of the information.
First write the configuration file: Create a DB.PROPERTIES file directly under SRC and write the content
<span style = "font-siZe: 18px;"> mysqldriver = com.mysql.jdbc.driver mysqlurl = jdbc/: mysql/: // localhost/: 3306/One user = root pwd = 123456 </</ span>
Then write a class code as follows as follows
<span style = "font-siZe: 18px;"> Package cn.java.ad; Import java.io.ioException; Import Java.SQL.Connection; Import Java.SQL.DriverManager; Import port java.sql.ResultSet; Import Java .sql.sqlexception; Import Java.Sql.Statement; Import Java.util.properties; /*** This example is a small package* @author hello* @Version jdk 1. 8 */ public Class Readmain {Static Properties POS = NULL; // Set static ones when loading the class only once static {pOS = New Properties (); // Establish pepperties to read the configuration file try {// Pos.load (thread.currentthread (). GetContextClassLoader (). GetResourceASStream ("db.properties"); race ();}} Public Static Connection Getcon () { // Establish a connection connection to TRY {class.FORNAME (POS.GetProperty ("MySQLDriver"); // Load com.mysql.jdbc.driver} Catch (ClassNotFoundexception E) Stacktrace ();} try {// load URL, User, Password Return DriverManager.getConnection (POS.GetProperty ("MySQLURL"), POS.GetProperty ("User"), Pos.getProperty);} Cat. ch (sqlexception e) {e.printstacktrace () ;} Return Null;} Public Static Void Close (ResultSet RS, Statement St, Connection Co) {Try {// The database connection is facilitated to encapsulate IF (RS! = NULL); i F ( st! = Null) st.close (); if (co! = null) Co.);} Catch (Exception E) {e.printstacktrace ();}} Public Static Void Close (ResultSet RS, Con, Con, Con, Next co) {try {// Close the resultSet connection if (RS! = NULL) rs.close (); if (co! = Null) co.close ();} Catch (Exception E) {e.printstacktrace ();} publ IC Static Void Close (Connection Co) {Try {// Close Connection if (Co! = Null) Co.Close ();} Catch (Exception E) {e.printstacktrace ();}} // SP AN "After writing the main code as follows
<span style = "font-siZe: 18px;"> Package cn.java.ad; Import java.sql.connection; Import java.sql.preparedStatement; Import Java.sql.ResultSet; mport java.sql.Statement; Public Class Main {public static void main (string [] args) {connection con = null; ResultSet reso; statement sta = null; string sql = null; string name = "Li Lei ";;" Male "; PreparedStatement PS = null; try {con = readmain.getcon (); SQL = "Insert Into Student (ID, name, Sex, Phone) Values (1235,?, 15896324131)" ""; );// /Get the SQL statement // Here, The First Parameter is 1, The Second is 2, ... // x the parameter value // It can be seen that the bidding is PS.SetString (1, name) starting from 1;/ /Insert the corresponding name into the ps.setString (2, sex); // Insert the corresponding sex into the ps.execute (); // execute the SQL statement without returning the salty.out.println ("" Insert ");} Catch (Exception E) {e.printstacktrace ();} Finally {Readmain.close (res, sta, con); // Turn off the connection}} </span>The following are the steps to establish db.properties
The above is all the contents of this article. I hope everyone can like it.