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 Demo {public static void main (string[] args) {test ();} public static void Test () {Connection con = null; PreparedStatement PS = null; ResultSet rs = null;try {//Register driver and establish connection con = jdbcutils.getconnection ();//create statement String sql = "Select Id,name,quantity,time,pri Ce from book ";p s = con.preparestatement (sql);//EXECUTE Statement rs = Ps.executequery ();/ * * These five columns are id,name,quantity,time,price * Why the most bitter feeling so sure of these five columns? * Because the SQL statements are written! * String sql = "Select Id,name,quantity,time,price from book"; * */system.out.println ("Number of columns:" + rs.getmetadata (). getColumnCount ());} catch (SQLException e) {throw new Daoexception (e);} finally {//release resource Jdbcutils.free (RS, ps, con);}}}
Results of console output
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 getColumnCount Get the number of columns in the result set