JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark to build more advanced tools and interfaces that enable database developers to write database applications. JDBC only to the major database manufacturers to provide a unified driver interface, the specific implementation by the major producers, each database manufacturers have different implementations, but we can use polymorphism, to achieve interface-oriented programming, without the control of database manufacturers specific implementation details. When using a specific database in a Java program, the corresponding jar package needs to be introduced first. For example, MySQL corresponds to the Mysql-connector-java-5.1.13-bin.jar, if it is Oracle and other databases need to introduce other jar package.
JDBC Establish connection (MySQL)
Build the properties file under the Classpath and load the file in the tool class Jdbctools
In the Jdbc.properties file:
1 driverclass = com.mysql.jdbc.Driver //This sentence represents the full class name of the database driver class 2 jdbcurl = jdbc:mysql:// localhost:3306/test //This is the configuration of the database to be accessed, test is the database name 3 user = root4 password = 123
Tool classes for establishing database connections and closing connections Jdbctools
1 Public classJdbctools {2 3 //Get database connection4 Public StaticConnection getconnection ()throwsException {5Connection Connection =NULL;6String Driverclass =NULL;7String URL =NULL;8String user =NULL;9String Password =NULL;Ten OneInputStream in = Jdbctools.class. getClassLoader (). getResourceAsStream ("Jdbc.properties");//load configuration file AProperties Properties =NewProperties (); - properties.load (in); - theDriverclass = Properties.getproperty ("Driverclass"); -url = properties.getproperty ("Jdbcurl"); -user = Properties.getproperty ("User"); -Password = properties.getproperty ("Password"); + - Class.forName (Driverclass);//Load driver class with full class name +Connection =drivermanager.getconnection (Url,user,password);//drivermanager can implement the same management for each database-driven class and get connections to the database A at returnconnection; - - } - - //Close Resource - Public Static voidReleaseresource (Statement statement,connection Connection) { in if(Statement! =NULL){ - Try{ to statement.close (); +}Catch(Exception e) { - e.printstacktrace (); the } * } $ if(Connection! =NULL){Panax Notoginseng Try{ - connection.close (); the}Catch(Exception E1) { + e1.printstacktrace (); A } the } + } - $ Public Static voidReleaseresource (ResultSet resultset,statement Statement, Connection Connection) { $ - if(ResultSet! =NULL){ - Try{ the resultset.close (); -}Catch(Exception e) {Wuyi e.printstacktrace (); the } - } Wu if(Statement! =NULL){ - Try{ About statement.close (); $}Catch(Exception e) { - e.printstacktrace (); - } - } A if(Connection! =NULL){ + Try{ the connection.close (); -}Catch(Exception E1) { $ e1.printstacktrace (); the } the } the}
JDBC to set up/close database connections