#JDBC驱动: In different databases, standard SQL statements can be ported, and database actual communication protocols and some database features are not portable, so there must be another layer between JDBC and database to map JDBC calls to specific database calls, this particular layer is the JDBC driver.
The common JDBC drivers are four kinds:
Jdbc-odbc Bridge, the first JDBC driver implemented to quickly promote JDBC, non multithreading, limited capabilities, this driver maps the JDBC API into an ODBC API
Directly maps the JDBC API to a database-specific client API, which contains local code for a particular database, and is available to clients in a particular database
"Support the three-tier structure of JDBC access mode, mainly for the applet phase, through the applet access to the database
"Pure Java, directly interacting with the database instance, intelligent, know the database using the underlying protocol, is currently the most popular JDBC driver
Java JDBC Connection Database code show:
The code is as follows |
Copy Code |
Import Java.io.InputStream; Import java.sql.Connection; Import Java.sql.DriverManager; Import Java.sql.ResultSet; Import java.sql.SQLException; Import java.sql.Statement; Import java.util.Properties; public class Jdbconnection { public Connection conn = null; Declaring an instance of a Connection object Public Statement stmt = null; Declaring an instance of a statement object public ResultSet rs = null; Declaring an instance of a ResultSet object To define a variable that saves a database drive private static String Dbclassname = "Com.microsoft.jdbc.sqlserver.SQLServerDriver"; private static String Dburl = "JDBC:MICROSOFT:SQLSERVER://LOCALHOST:1433;DATABASENAME=DB_ATM"; private static String Dbuser = "sa"; private static String dbpwd = "sa"; Public jdbconnection (String propertyfilename) {//method of constructing with attribute file name Properties prop = new properties ();//Property Collection Object InputStream is = null; try { is = JDBConnection.class.getClassLoader (). getResourceAsStream ( Propertyfilename);//property file input stream is = new FileInputStream ("src/" + propertyfilename); Prop.load (IS);//Load the property file stream into the Properties object Is.close ();//Close stream Dbclassname = Prop.getproperty ("Dbclassname"); Dburl = Prop.getproperty ("Dburl"); Dbuser = Prop.getproperty ("Dbuser"); Dbpwd = Prop.getproperty ("Dbpwd"); catch (Exception e) { System.out.println ("Property file" + Propertyfilename +) Open failed! "); } try { Class.forName (dbclassname);//1. Register Driver catch (ClassNotFoundException e) { E.printstacktrace (); } } Public jdbconnection () {//default constructor with no parameters try { Class.forName (dbclassname);//1. Register Driver catch (ClassNotFoundException e) { E.printstacktrace (); } } public static Connection getconnection () { Connection conn = null; try { Class.forName (dbclassname);//1. Register Driver 2. Establish a link with the database conn = Drivermanager.getconnection (Dburl, Dbuser, dbpwd); catch (Exception ee) { Ee.printstacktrace (); } if (conn = = null) { System.err.println ("Warning: dbconnectionmanager.getconnection () failed to get the database link. \r\n\r\n LINK Type: " + Dbclassname + "\ r \ n Link Location:" + Dburl + "\ r \ n User/password" + Dbuser + "/" + dbpwd); } Return conn; } /* * Function: Execute query statement */ Public ResultSet executequery (String sql) { try {//catch exception Call the Getconnection () method to construct an instance of the connection object conn conn = getconnection (); stmt = Conn.createstatement (RESULTSET.TYPE_SCROLL_INSENSITIVE,//3. Create statement RESULTSET.CONCUR_READ_ONLY); rs = stmt.executequery (sql);//4. Execute Query catch (SQLException ex) { System.err.println (Ex.getmessage ()); Output exception information } Return RS; Returns the result set object 5. Results processing } /* * Function: Perform update operation */ public int executeupdate (String sql) { int result = 0; To define a variable that holds the return value try {//catch exception Call the Getconnection () method to construct an instance of the connection object conn conn = getconnection (); stmt = Conn.createstatement (resultset.type_scroll_insensitive, RESULTSET.CONCUR_READ_ONLY); result = Stmt.executeupdate (SQL); Perform an update operation catch (SQLException ex) { result = 0; Assign a variable that holds the return value to 0 } return result; Returns the variable that holds the return value } /* * Function: Close the connection of the database */ public void Close () {//6. Releasing resources try {//catch exception try { if (Rs!= null) {//When the instance Rs of the ResultSet object is not empty Rs.close (); Close ResultSet Object } finally { try { if (stmt!= null) {//When an instance of the statement object stmt is not empty Stmt.close (); Close Statement Object } finally { IF (conn!= null) {//When an instance of the Connection object conn is not empty Conn.close (); Close Connection Object } } } catch (Exception e) { E.printstacktrace (System.err); Output exception information } } } |
Some of the property files you need to be aware of:
Dbclassname=com.microsoft.jdbc.sqlserver.sqlserverdriver
Dbclassname2=com.mysql.jdbc.driver
Dbpwd=sa
Dbpwd2=root
Dburl=jdbc\:microsoft\:sqlserver\://localhost\:1433;databasename\=db_atm
Dburl2=jdbc\:mysql\://localhost\:3306/db_atm
Dbuser=sa
Dbuser2=root