JDBC Connection Database Operations
JDBC Connection database template, favorite can do template use (small engineering, general large projects will use the framework, C3P0 and other connections, regardless of this method!) )。
the use of configuration files (using the configuration file can make our later changes more convenient, of course, can also use the Java enumeration effect is the same, but we recommend that you use the configuration file )
Here you use MySQL to do the subclasses:
1 #db. Properties 2 #一般放在src下 3 4 5 6 driver=com.mysql.jdbc.Driver7 url=jdbc:mysql://127.0.0.1:3306/ Duomao?useunicode=true&characterencoding=utf-88 user=root9 password=* *********
About Useunicode and charsetencoding (Duomao is the name of the database)
1. When data is stored:
when storing the project data, the database decodes the data into bytecode using the UTF-8 format, and then the decoded bytecode is reused in the database using the GBK encoding.
2. When data is taken:
when fetching data from the database, the database will first decode the data in the database into bytecode in GBK format, then encode the decoded bytecode again in UTF-8 format, and then return the data to the client.
To read a configuration file and make a connection operation:
Create a toolkit that you can use repeatedly:
The approximate process:
1: Use properties to read configuration files
2: Load Driver
3: Get connection conn=drivermanager.getconnection (URL, username, password);
4: Remember to use the. Close method to close the connection to the database after you have finished using it!
1 ImportJava.io.FileReader;2 Importjava.io.IOException;3 ImportJava.io.Reader;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 Importjava.sql.SQLException;7 Importjava.util.Properties;8 9 Ten Public classDbutil { One Private StaticString driver; A Private StaticString URL; - Private StaticString username; - Private StaticString password; the - //reading the contents of a configuration file - Static{ -Properties Properties =NewProperties (); + Reader in; - Try { +in =NewFileReader ("Db.properties"); A //The load method loads the property list into the Properties class object from the file input stream corresponding to the. Properties Property File at properties.load (in); -}Catch(IOException e) { - e.printstacktrace (); - } - //extract the parameters from the configuration file -Driver = Properties.getproperty ("Driver"); inurl = properties.getproperty ("url"); -Username = Properties.getproperty ("username"); toPassword = properties.getproperty ("Password"); + } - the Public StaticConnection Open () { * Try { $ Class.forName (driver);Panax Notoginseng returndrivermanager.getconnection (URL, username, password); -}Catch(ClassNotFoundException e) { the e.printstacktrace (); +}Catch(SQLException e) { A e.printstacktrace (); the } + return NULL; - } $ $ Public Static voidClose (Connection conn) { - if(Conn! =NULL){ - Try { the conn.close (); -}Catch(SQLException e) {Wuyi e.printstacktrace (); the } - } Wu } -}
JDBC Connection Database Toolkit template