最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaweb中mysql數(shù)據(jù)庫連接步驟方法及其實例

 更新時間:2017年04月17日 15:21:38   投稿:wbb  
這篇文章主要介紹了使用java web 連接MySQL數(shù)據(jù)庫的驅(qū)動方法的相關知識,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下

一、直接連接,不封裝到工具類中,主要步驟:

先導包:mysql-connector-java-5.0.8-bin.jar(點擊跳轉(zhuǎn)到下載界面),放在WebRoot/WEB-INF/lib/下

1.加載驅(qū)動//com.MySQL.jdbc.Driver

2.獲取連接 Connection對象

3.獲取用于向數(shù)據(jù)庫發(fā)送SQL的Statement對象

4.執(zhí)行sql,獲取數(shù)據(jù),解析數(shù)據(jù)

5.關閉連接,釋放資源

/*協(xié)議:子協(xié)議://主機:端口/數(shù)據(jù)庫名*/
Stringurl="jdbc:mysql://localhost:3306/jdbctest";
//mysql數(shù)據(jù)庫的用戶名與密碼,安裝時自己設置,一般默認為root
Stringuser="root";
Stringpassword="root";
Connectionconnection=null;
Statementstatement=null;
ResultSetresultSet=null;
try{
//1.加載驅(qū)動//com.mysql.jdbc.Driver
/*
*DriverManager.registerDriver(new
*Driver());用這種方法會加載兩次驅(qū)動,也就是說會創(chuàng)建兩個drive對象
*/
Class.forName("com.mysql.jdbc.Driver");
//2.獲取連接
connection=DriverManager.getConnection(url,user,password);
//3.獲取用于向數(shù)據(jù)庫發(fā)送SQL的Statement對象
statement=connection.createStatement();
//4.執(zhí)行sql,獲取數(shù)據(jù)
resultSet=statement.executeQuery("SELECT*FROMusers;");
//解析數(shù)據(jù)
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;
}
/* 協(xié)議:子協(xié)議://主機:端口/數(shù)據(jù)庫名 */
String url = "jdbc:mysql://localhost:3306/jdbctest";
// mysql數(shù)據(jù)庫的用戶名與密碼,安裝時自己設置,一般默認為root
String user = "root";
String password = "root";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
  // 1.加載驅(qū)動//com.mysql.jdbc.Driver
  /*
   * DriverManager.registerDriver(new
   * Driver());用這種方法會加載兩次驅(qū)動,也就是說會創(chuàng)建兩個drive對象
   */
  Class.forName("com.mysql.jdbc.Driver");
  // 2.獲取連接
  connection = DriverManager.getConnection(url, user, password);
  // 3.獲取用于向數(shù)據(jù)庫發(fā)送SQL的Statement對象
  statement = connection.createStatement();
  // 4.執(zhí)行sql,獲取數(shù)據(jù)
  resultSet = statement.executeQuery("SELECT * FROM users;");
  // 解析數(shù)據(jù)
  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;
  }
}

二、將數(shù)據(jù)庫連接封裝成一個工具類

這樣做的好處是,在實際開發(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"));
//加載驅(qū)動
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{
//調(diào)用工具類中的靜態(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{
//調(diào)用工具類中的靜態(tài)方法來關閉連接,釋放資源
JdbcUtil.releaseConn(connection,statement,resultSet);
}

希望本文可以對需要的朋友有幫助

相關文章

最新評論

阿城市| 崇义县| 海晏县| 绥中县| 鱼台县| 纳雍县| 抚顺市| 莫力| 桦川县| 都江堰市| 东丽区| 通江县| 金塔县| 拉孜县| 墨竹工卡县| 武威市| 牟定县| 三亚市| 鹤峰县| 集安市| 杭锦后旗| 固镇县| 沂水县| 通化县| 甘泉县| 昆山市| 奇台县| 曲麻莱县| 罗江县| 河北省| 枣阳市| 盐亭县| 五台县| 香格里拉县| 电白县| 吉林省| 肥城市| 鲁甸县| 灵寿县| 临洮县| 渭源县|