JDBC.java
~~~
package zyw.tools;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBC {
private static String driver;//快捷鍵Ctrl+Art+f
private static String url;
private static String username;
private static String password;
static {
try {
ClassLoader classLoader = JDBC.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
Properties properties=new Properties();
properties.load(resourceAsStream);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
//注冊驅動,獲取連接
try {
/* Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://122.14.200.136:3306/javadb";
connection = DriverManager.getConnection(url, "root", "6a133f0024");*/
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
//釋放資源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
~~~
調用方法
~~~
Connection connection =JDBC.getConnection();
JDBC.release(connection,preparedStatement,resultSet);
~~~
db.properties文件放在src目錄下
~~~
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://122.14.200.136:3306/javadb
username=root
password=xxx
~~~
- 學習心得
- 將jar包加入項目中
- Servlet
- 編寫第一個Servlet
- 使用Servlet3.0創建Servlet
- HttpServletRequest簡介-獲取請求行和請求頭
- HttpServletResponse獲取請求參數
- response對象發送響應行和響應頭
- HttpServletResponse發送請求體
- 【實例】驗證碼
- Servlet的生命周期
- ServletConfig對象
- ServletContext-獲取項目初始化參數
- ServletContext對象-在多個Servlet之間共享參數
- 請求轉發
- 【實例】登錄錯誤時顯示錯誤界面
- 重定向介紹
- 網頁的自動刷新
- Servlet線程安全
- 案例 文件下載
- Cookie與Session會話技術
- 會話技術概述
- Cookie的會話流程
- 獲取Cookie
- 【案例 】記錄網站上一次訪問時間
- Session的會話流程
- 使用Session域對象存取數據
- Session的生命周期和持久化
- 【實例】購物車的簡單使用
- JSP技術
- JSP指令
- JSP隱式對象
- JSP標簽
- JSTL標簽庫
- EL表達式
- EL的內置對象和執行表達式
- JSTL簡介
- JSTL-if標簽
- JSTL-forEach標簽
- 【案例】實現商品列表展示
- MySQL數據庫
- SQL語句
- 命名規則與數據類型
- SQL-增刪改查
- 設計數據庫
- JDBC的應用
- JDBC連接數據庫
- 查詢
- 插入.刪除.修改
- Junit單元測試
- 預防SQL注入
- JDBCutils工具類
- 監聽器Listener
- ServletContextListener
- HttpSessionListener和ServletRequestListener
- 域對象屬性監聽器
- 對象感知監聽器
- 【案例】商品促銷活動推廣
- 過濾器Filter
- 創建一個過濾器
- Filter生命周期和配置
- 【案例】解決中文輸出亂碼問題
- XML入門
- XML元素和解析方式
- XML約束
- 實戰-生鮮后臺管理系統
- MVC和三層架構
- 項目需求和項目搭建
- 數據庫設計
- 注冊功能
- 登錄功能
- 記住密碼
- BeanUtils的使用
- Servlet的抽取(上)
- Servlet的抽取(下)
- 增加生鮮種類
- 查詢生鮮列表
- 分頁功能
- 修改生鮮信息
- 刪除生鮮功能
- 權限控制Filter