Java_JDBC _ use the method of reading the configuration file to connect to the database, java_jdbc configuration file
1 package com. homewoek3_4.dao; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. SQL. connection; 6 import java. SQL. driverManager; 7 import java. SQL. preparedStatement; 8 import java. SQL. resultSet; 9 import java. SQL. SQLException; 10 import java. util. properties; 11 12/** 13 * General database connection class 14 * @ author Administrator 15 */16 public abstract class BaseDao {17 private static final String path = "database. properties "; 18 private static String DB_DRIVER; 19 private static String DB_URL; 20 private static String DB_USER; 21 private static String DB_PWD; 22 23 protected Connection conn = null; 24 protected PreparedStatement ps = null; 25 protected ResultSet rs = null; 26 27 static {28 Properties pro = new Properties (); 29 InputStream io = BaseDao. class. getClassLoader (). getRes OurceAsStream (path); 30 try {31 // read the configuration file 32 pro. load (io); 33} catch (IOException e) {34 e. printStackTrace (); 35} 36 DB_DRIVER = pro. getProperty ("DB_DRIVER"); 37 DB_URL = pro. getProperty ("DB_URL"); 38 DB_USER = pro. getProperty ("DB_USER"); 39 DB_PWD = pro. getProperty ("DB_PWD"); 40 try {41 // load the Driver Class 42 Class. forName (DB_DRIVER); 43} catch (ClassNotFoundException e) {44 e. printStackTrace (); 45} 4 6} 47/** 48 * Open Database Connection 49 */50 protected void openConn () {51 try {52 conn = DriverManager. getConnection (DB_URL, DB_USER, DB_PWD); 53} catch (SQLException e) {54 e. printStackTrace (); 55} 56} 57/** 58 * close database connection 59 */60 protected void closeConn () {61 try {62 if (rs! = Null) {63 rs. close (); 64} 65 if (ps! = Null) {66 ps. close (); 67} 68 if (conn! = Null) {69 conn. close (); 70} 71} catch (SQLException e) {72 e. printStackTrace (); 73} 74} 75/** 76 * add, delete, and modify 77 * @ param SQL 78 * @ param obj 79 * @ return 80 */81 protected int executeUpdate (String SQL, object... obj) {82 int result =-1; 83 this. openConn (); 84 try {85 ps = conn. prepareStatement (SQL); 86 if (obj! = Null) {87 for (int I = 0; I <obj. length; I ++) {88 ps. setObject (I + 1, obj [I]); 89} 90} 91 result = ps.exe cuteUpdate (); 92} catch (SQLException e) {93 e. printStackTrace (); 94} 95 return result; 96} 97/** 98 * query 99 * @ param sql100 * @ param obj101 * @ return102 */103 protected void executeQuery (String SQL, Object... obj) {104 this. openConn (); 105 try {106 ps = conn. prepareStatement (SQL); 107 if (Obj! = Null) {108 for (int I = 0; I <obj. length; I ++) {109 ps. setObject (I + 1, obj [I]); 110} 111} 112 rs = ps.exe cuteQuery (); 113} catch (SQLException e) {114 e. printStackTrace (); 115} 116} 117}
Create the database. properties file. Note: The suffix must be properties. This format changes the icon in MyEclipse, which is easy to write incorrectly.
1 DB_DRIVER=com.mysql.jdbc.Driver2 DB_URL=jdbc:mysql://localhost:3306/Pet?useUnicode=true&characterEncoding=utf8&useSSL=true3 DB_USER=root4 DB_PWD=111111