Jdbc-mysql Basic Registration Driver Class.forName

Source: Internet
Author: User
Tags getmessage stub throwable

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. Online about the database operation code, look good, to the most bitter code here is the same. Meet the choice, or ashamed to ask.
Javase:8
mysql:5.7.14
mysql-connector-java:5.1.44
Os:windows7 x64
Ide:myeclipse

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}}


Code for the 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);}}}}

Data presentation in the database

Test class Code

Package Com.jizuiku;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet;import java.sql.sqlexception;/** * Demo class *  * @author Blog Park-to the most bitter * @version V2017.11.13 */public class Demo {public static void Mai N (string[] args) {Connection conn = null; PreparedStatement ps= null; ResultSet rs = null;try {//GET link conn = jdbcutils.getconnection ();//To write the column name, the purpose is to facilitate subsequent fetch data  String sql = "Select Id,name,qu Antity,time,price from book ";p s = conn.preparestatement (sql);//EXECUTE statement because the executed statement returns more than a row of multiple columns, so use the ExecuteQuery statement rs = Ps.executequery ();//query and output data while (Rs.next ()) {System.out.println (Rs.getobject ("id") + "" +    Rs.getobject ("name") + "" +    Rs.getobject ("quantity") + "" "+    Rs.getobject (" time ") +" "+    Rs.getobject (" Price ");}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new Daoexception (E.getmessage (), e);} Release Resource Jdbcutils.free (Rs,ps,conn);}}

Effect Show

After reading the presentation, did you ever have this question: why? Class.forName () can register the driver, what principle? Please see below to decompose.

Find source code

Open it and see

/* Copyright (c) 2002, Oracle and/or its affiliates.  All rights reserved. The MySQL connector/j is licensed under the terms of the GPLv2 

The class is loaded into the virtual machine by the name of the class. A load invokes the static code block of the class, which is the mechanism for registering the

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 Basic Registration Driver Class.forName

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.