真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

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

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

我們提供的服務有:成都網站設計、網站制作、外貿營銷網站建設、微信公眾號開發(fā)、網站優(yōu)化、網站認證、梁子湖ssl等。為1000+企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的梁子湖網站制作公司

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

1.加載驅動//com.MySQL.jdbc.Driver

2.獲取連接 Connection對象

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

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

5.關閉連接,釋放資源

/*協(xié)議:子協(xié)議://主機:端口/數(shù)據庫名*/
Stringurl="jdbc:mysql://localhost:3306/jdbctest";
//mysql數(shù)據庫的用戶名與密碼,安裝時自己設置,一般默認為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.獲取用于向數(shù)據庫發(fā)送SQL的Statement對象
statement=connection.createStatement();
//4.執(zhí)行sql,獲取數(shù)據
resultSet=statement.executeQuery("SELECT*FROMusers;");
//解析數(shù)據
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ù)據庫名 */
String url = "jdbc:mysql://localhost:3306/jdbctest";
// mysql數(shù)據庫的用戶名與密碼,安裝時自己設置,一般默認為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.獲取用于向數(shù)據庫發(fā)送SQL的Statement對象
  statement = connection.createStatement();
  // 4.執(zhí)行sql,獲取數(shù)據
  resultSet = statement.executeQuery("SELECT * FROM users;");
  // 解析數(shù)據
  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ù)據庫連接封裝成一個工具類

這樣做的好處是,在實際開發(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);
}

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


文章名稱:javaweb中mysql數(shù)據庫連接步驟方法及其實例
標題路徑:http://weahome.cn/article/pecpjd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部