资源文件
资源文件properties: url=jdbc:mysql://localhost/studentManagement?useUnicode=true&characterEncoding=utf-8 driver=com.mysql.jdbc.Driver userName=root password=root 工具程序: package com.dawenzi.conn; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; /**
- Created by root on 16-12-14. */ public class ConnectionDatabase { private static Properties properties=new Properties(); static { try { properties.load(ConnectionDatabase.class.getResourceAsStream(“ConnectionMessage.properties”)); } catch (IOException e) { e.printStackTrace(); } } public Connection conn; public PreparedStatement pst=null; public String sql=null; public void createConn(){ try { Class.forName(properties.getProperty(“driver”)); conn= DriverManager.getConnection( properties.getProperty(“url”), properties.getProperty(“userName”), properties.getProperty(“password”));
if (sql!=null){ pst=conn.prepareStatement(sql); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public void close(){ try { this.conn.close(); if (sql!=null){ pst1.close(); } } catch (SQLException e) { e.printStackTrace(); } } }