javaweb中mysql數據庫連接步驟方法及其實例
更新時間:2017年04月17日 15:21:38 投稿:wbb
這篇文章主要介紹了使用java web 連接MySQL數據庫的驅動方法的相關知識,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
一、直接連接,不封裝到工具類中,主要步驟:
先導包:mysql-connector-java-5.0.8-bin.jar(點擊跳轉到下載界面),放在WebRoot/WEB-INF/lib/下
1.加載驅動//com.MySQL.jdbc.Driver
2.獲取連接 Connection對象
3.獲取用于向數據庫發(fā)送SQL的Statement對象
4.執(zhí)行sql,獲取數據,解析數據
5.關閉連接,釋放資源
/*協議:子協議://主機:端口/數據庫名*/
Stringurl="jdbc:mysql://localhost:3306/jdbctest";
//mysql數據庫的用戶名與密碼,安裝時自己設置,一般默認為root
Stringuser="root";
Stringpassword="root";
Connectionconnection=null;
Statementstatement=null;
ResultSetresultSet=null;
try{
//1.加載驅動//com.mysql.jdbc.Driver
/*
*DriverManager.registerDriver(new
*Driver());用這種方法會加載兩次驅動,也就是說會創(chuàng)建兩個drive對象
*/
Class.forName("com.mysql.jdbc.Driver");
//2.獲取連接
connection=DriverManager.getConnection(url,user,password);
//3.獲取用于向數據庫發(fā)送SQL的Statement對象
statement=connection.createStatement();
//4.執(zhí)行sql,獲取數據
resultSet=statement.executeQuery("SELECT*FROMusers;");
//解析數據
while(resultSet.next()){
intid=resultSet.getInt("id");
Stringname=resultSet.getString("name");
Stringpsd=resultSet.getString("password");
Stringemail=resultSet.getString("email");
Stringbirthday=resultSet.getString("birthday");
System.out.println(id+""+name+""+psd+""+email
+""+birthday);
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
//5.關閉連接,釋放資源
if(resultSet!=null){
try{
resultSet.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
resultSet=null;
}
if(statement!=null){
try{
statement.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
statement=null;
}
if(connection!=null){
try{
connection.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
connection=null;
}
/* 協議:子協議://主機:端口/數據庫名 */
String url = "jdbc:mysql://localhost:3306/jdbctest";
// mysql數據庫的用戶名與密碼,安裝時自己設置,一般默認為root
String user = "root";
String password = "root";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 1.加載驅動//com.mysql.jdbc.Driver
/*
* DriverManager.registerDriver(new
* Driver());用這種方法會加載兩次驅動,也就是說會創(chuàng)建兩個drive對象
*/
Class.forName("com.mysql.jdbc.Driver");
// 2.獲取連接
connection = DriverManager.getConnection(url, user, password);
// 3.獲取用于向數據庫發(fā)送SQL的Statement對象
statement = connection.createStatement();
// 4.執(zhí)行sql,獲取數據
resultSet = statement.executeQuery("SELECT * FROM users;");
// 解析數據
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String psd = resultSet.getString("password");
String email = resultSet.getString("email");
String birthday = resultSet.getString("birthday");
System.out.println(id + " " + name + " " + psd + " " + email
+ " " + birthday);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.關閉連接,釋放資源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resultSet = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = null;
}
}
二、將數據庫連接封裝成一個工具類
這樣做的好處是,在實際開發(fā)中,就能做到,改一處即可修改全局。
1.建一個名為db.properties的配置文件,放于src/
url=jdbc:mysql://localhost:3306/jdbctest username=root password=root driver=com.mysql.jdbc.Driver
2.工具類:
importjava.io.IOException;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
publicclassJdbcUtil{
//私有靜態(tài)變量,用以讀取配置文件
privatestaticPropertiesconfig=newProperties();
static{
try{
//配置資源文件
config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
//加載驅動
Class.forName(config.getProperty("driver"));
}catch(IOExceptione){
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
publicstaticConnectiongetConnection(){
Connectionconnection=null;
try{
connection=DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));
}catch(SQLExceptione){
e.printStackTrace();
}
returnconnection;
}
//用以關閉連接,釋放資源
publicstaticvoidreleaseConn(Connectionconnection,Statementstatement,
ResultSetresultSet){
if(resultSet!=null){
try{
resultSet.close();
}catch(SQLExceptione){
e.printStackTrace();
}
resultSet=null;
}
if(statement!=null){
try{
statement.close();
}catch(SQLExceptione){
e.printStackTrace();
}
statement=null;
}
if(connection!=null){
try{
connection.close();
}catch(SQLExceptione){
e.printStackTrace();
}
connection=null;
}
}
}
3.使用實例:
Connectionconnection=null;
Statementstatement=null;
ResultSetresultSet=null;
try{
//調用工具類中的靜態(tài)方法來獲取連接
connection=JdbcUtil.getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery("select*fromusers");
while(resultSet.next()){
intid=resultSet.getInt("id");
Stringname=resultSet.getString("name");
Stringpsd=resultSet.getString("password");
Stringemail=resultSet.getString("email");
Stringbirthday=resultSet.getString("birthday");
System.out.println(id+""+name+""+psd+""+email
+""+birthday);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//調用工具類中的靜態(tài)方法來關閉連接,釋放資源
JdbcUtil.releaseConn(connection,statement,resultSet);
}
希望本文可以對需要的朋友有幫助
相關文章
利用Spring Cloud Zuul實現動態(tài)路由示例代碼
Spring Cloud Zuul路由是微服務架構的不可或缺的一部分,提供動態(tài)路由,監(jiān)控,彈性,安全等的邊緣服務。下面這篇文章主要給大家介紹了關于利用Spring Cloud Zuul實現動態(tài)路由的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
SpringBoot項目中枚舉類型字段與前端和數據庫的交互方法
最近做的這個項目中,用到了大量的枚舉類,下面這篇文章主要給大家介紹了關于SpringBoot項目中枚舉類型字段與前端和數據庫的交互方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07

