Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See Root 321, rivers with one.
Meekness Conscience Lord, willing to do without regrets to the most bitter. Reading exercise strong body and mind, Prudential advised and the line and cherish.
Data,
data, Lingen on the data. Cloud computing, AI and other technologies are based on data. You must be cautious about operating the database. Give the most bitter code here, and take a look at it, to have your own judgment. Encounter a choice, or ashamed to ask, think twice.
Javase:8
mysql:5.7.14
mysql-connector-java:5.1.44
mysql-front:5.4
Os:windows7 x64
Ide:myeclipse 2017
Special exception classes
Package com.jizuiku;/** * This class is important, that is, to complete the action of throwing exceptions, by compiling, but also to keep the interface of the data logic layer concise. * * @author Blog Park-to the most bitter * @version V17.11.08 */public class Daoexception extends RuntimeException {/** * */private STA Tic final Long serialversionuid = 1l;public daoexception () {//TODO auto-generated constructor stub}public daoexception (St Ring message) {super (message);//TODO auto-generated constructor stub}public daoexception (Throwable cause) {Super (cause );//TODO auto-generated constructor stub}public daoexception (String message, Throwable cause) {Super (message, cause);// TODO Auto-generated Constructor stub}public daoexception (String message, Throwable cause, Boolean enablesuppression, Boolean writablestacktrace) {super (message, cause, enablesuppression, writablestacktrace);//TODO auto-generated Constructor stub}}
Jdbcutils class
Package Com.jizuiku;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import java.sql.sqlexception;/** * * * @author to most bitter * @version V17.11.07 */public final class J dbcutils {/** * URL format, JDBC: Sub-Protocol: Sub-name//hostname: Port number/database name? property Name = Property Value & Property name = attribute value * There are multiple parameters in the configstring variable, which requires deep study of their specific meanings */ private static String configstring = "? useunicode=true&characterencoding=utf8&usessl=true";p rivate static String url = "Jdbc:mysql://localhost:3306/jdbcforjava" + configstring;//local MySQL database (No child name) port number 3306 Database jdbcforjavaprivate static String user = "root";p rivate static string password = "";//tool class, direct use, not raw object private jdbcutils () {}//registered driver, where the static code block is applied only once the feature static {try {Class.forName ("Com.mysql.jdbc.Driver");} catch ( ClassNotFoundException e) {//TODO auto-generated catch Blockthrow new Exceptionininitializererror (e);}} /** * Get a link to a specified database * */public static Connection getconnection () throws SQLException {return Drivermanager.getconnection (url, user, password);} /** * Frees three resources ResultSet preparedstatement Connection * */public static void Free (ResultSet rs, PreparedStatement PS, Connec tion con) {try {if (rs! = null) {Rs.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new Daoexce Ption (E.getmessage (), E); Finally {try {if (PS! = null) {Ps.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new daoexcept Ion (E.getmessage (), e);} Finally {try {if (con! = null) {Con.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new Daoexce Ption (E.getmessage (), E);}}}}
Content in the database
Test class
Package Com.jizuiku;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet;import Java.sql.resultsetmetadata;import Java.sql.sqlexception;import Java.util.arraylist;import Java.util.Arrays;import Java.util.hashmap;import Java.util.list;import java.util.map;/** * * * @author Blog Park-to the most bitter * @version V17.11.09 */public CLA SS Demo {public static void main (string[] args) {//Note that there is a hole, name as username//column name and column alias String sql = "Select Id,name As Username,quantity,time,price from book "; list<map<string,object>> result = Test (SQL); SYSTEM.OUT.PRINTLN ("Query to result set is:"); SYSTEM.OUT.PRINTLN (result);} public static list<map<string,object>> test (String sql) {Connection con = null; PreparedStatement PS = null; ResultSet rs = null; ResultSetMetaData RSMD = null;try {//Register driver and establish connection con = jdbcutils.getconnection ();//Create statement and do some preprocessing on SQL statement PS = Con.preparestat Ement (SQL);//EXECUTE Statement rs = Ps.executequery ();//Gets the number of columns RSMD = Rs.getmetadata (); int colcount = Rsmd.getcolumncount ();//Because the name of the column is to be stored, so it is the same as the number of columns string[] colnames = new string[colcount];/* the name above * Getcolumnlabel alias * Getcolumnlabel () method index is starting from 1. For the most bitter writing starting from 0, the exception is thrown. */for (int i = 1; I <= colcount; i++) {colnames[i-1] = Rsmd.getcolumnlabel (i);} System.out.println ("The array contents of the alias that stores the column is:"); System.out.println (arrays.tostring (colnames)); The generic declarations here are also tricky. Hashmap<string,object> data = null; list<map<string,object>> datas = new arraylist<map<string,object>> ();// Load the result of the query into the datas while (Rs.next ()) {data = new hashmap<string, object> (); for (int i = 0; i < ColCount; i++) {// Key Valuedata.put (Colnames[i], Rs.getobject (Colnames[i]));} Datas.add (data);} Return the result return datas;} catch (SQLException e) {throw new Daoexception (e);} finally {//release resource Jdbcutils.free (RS, ps, con);}}}
Results of console output
The array contents of the alias of the stored column are: [ID, username, quantity, time, price] The result set queried is: [{quantity=170, price=60.00, id=2, time=2017-11-06 00:00:00.0, Username= Zhuangzi}, {quantity=1020, price=99.87, id=4, time=2017-11-10 00:00:00.0, Username= Mencius}, {quantity=170, price=90.00, id=5, time=2017-11-03 00:00:00.0, username= typhoid Miscellaneous diseases}, {quantity=60, price=99.99, id=6, time=2017-11-16 00:00:00.0, Username= Mencius}, {quantity=190, price=110.00, id=7, time=2017-11-01 00:00:00.0, Username= University}, {quantity=200, price=40.00, Id=8, time=2017-11-12 00:00:00.0, Username= Zhouyi}, {quantity=195, price=40.00, id=9, time=2017-10-10 00:00:00.0, username=}, {quantity=185, price=45.00, id=10, time=2017-09-09 00:00:00.0, Username= Diamond Sutra}, {quantity=200 , price=99.99, id=13, time=2017-01-01 00:00:00.0, Username= Buddha-sutra}, {quantity=130, price=99.87, id=14, time=2017-11-08 00:00:00.0, Username= Mencius}, {quantity=60, price=99.99, id=17, time=2017-11-16 00:00:00.0, Username= Mencius}, {quantity=60, price=99.99, id=18, time=2017-11-16 00:00:00.0, Username= Mencius}, {quantity=60,price=99.99, id=19, time=2017-11-16 00:00:00.0, Username= Mencius}]
Note: This blog post has a lot of small knowledge points, you can see more. Then, the operation of the database to be careful, give the most bitter code here to see just fine.
Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.
The Jdbc-mysql base encapsulates the resulting set of queries into a map form