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);}}}}
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;/** * * * @author Blog Park-to the most bitter * @version V17.11.09 */public class De mo {public static void main (string[] args) {//gives the most bitter name alias String sql = "Select Id,name as Username,quantity,time,price F Rom book "; Test (SQL);} public static void 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);//Perform rs = Ps.executequery ();//Look at the information in the results RSMD = Rs.getmetadata ();//Get the number of columns int colcount = Rsmd.getcolumncount () ;//getcolumnname column name Getcolumnlabel alias//NOTE: I starting from 1 for (int i = 1; I <= colcount; i++) {System.out.print (Rsmd.getcolu Mnclassname (i) + ""); System.out.print (Rsmd.getcolumnname (i) + ""); System.out.println (Rsmd.getcolumnlabel (i));}} catch (SQLException e) {throw new DaoException (e);} finally {//release resource Jdbcutils.free (RS, ps, con);}}}
Types of fields in the database
Test the output of the code in the console
Note: 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.
Jdbc-mysql base ResultSetMetaData getcolumnname getcolumnlabel get column names and aliases