知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 網(wǎng)站建設(shè) >
Java Web開發(fā)中使用Mysql數(shù)據(jù)庫(kù)
發(fā)表時(shí)間:2017-3-14
發(fā)布人:葵宇科技
瀏覽次數(shù):40
開發(fā)模式使用的是JSP+Servlet+JavaBean開發(fā)模式
第一步 裝好數(shù)據(jù)庫(kù)。并在數(shù)據(jù)庫(kù)中建好需要使用的數(shù)據(jù)庫(kù)以及表。
第二步 將數(shù)據(jù)庫(kù)連接jar包導(dǎo)入到工程文件中。Eclipse是導(dǎo)入到WebContent/WEB-INF/lib下,MyEclipse是導(dǎo)入到Webroot/WEB-INF/lib下。我用的是mysql-connector-java-5.1.6-bin.jar這個(gè)jar包。(此包極為重要!~)
第三步 注冊(cè)驅(qū)動(dòng)。對(duì)數(shù)據(jù)庫(kù)進(jìn)行各種操作的時(shí)候,都必須通過JDBC建立應(yīng)用程序與數(shù)據(jù)庫(kù)的連接。下面給出連接函數(shù)以及釋放連接函數(shù):
public Connection getConnection() //數(shù)據(jù)庫(kù)連接函數(shù)
{
Connection conn=null;
String driver="com.mysql.jdbc.Driver";
String dburl="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8"; //mydb為你的數(shù)據(jù)庫(kù)名稱
String username="root"; //你的mysql用戶名
String password="root"; //你的mysql登錄密碼
try{
Class.forName(driver); //加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序
conn=DriverManager.getConnection(dburl,username,password);
}catch(Exception e){e.printStackTrace();}
return conn;
}
public void JdbcFree(Connection conn,Statement st,ResultSet rs) //數(shù)據(jù)庫(kù)連接釋放函數(shù)
{
try { if (rs != null) rs.close();}
catch (SQLException e) {e.printStackTrace();}
finally {
try { if (st != null) st.close();}
catch (SQLException e) {e.printStackTrace();}
finally {
try {if (conn != null) conn.close();}
catch (SQLException e) {e.printStackTrace();}
}
}
}
第四步 在DAO方法中寫需要的數(shù)據(jù)庫(kù)操作代碼,例如登錄的代碼:
public boolean searchUser(UserBean user){
//按用戶名和密碼校驗(yàn)用戶是否合法
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rst=null;
try{
conn=getConnection();
String strsql="select * from usertable where username=? and password=? and type=?";
pstmt=conn.prepareStatement(strsql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getUsertype());
rst=pstmt.executeQuery();
if(rst.next()){
return true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}finally{ //釋放資源
JdbcFree(conn,pstmt,rst);
}
return false;
}
第五步 在Servlet中處理DAO方法中的返回值,例如登錄成功,則存儲(chǔ)Session然后轉(zhuǎn)到登錄成功之后的頁(yè)面。登錄失敗則提示登錄失敗。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南小程序開發(fā)報(bào)價(jià)
- 軟件定制公司
- 百度小程序開發(fā)公司
- 跳轉(zhuǎn)小程序
- 網(wǎng)站上首頁(yè)
- 企業(yè)網(wǎng)站
- 關(guān)鍵詞快速排名
- 報(bào)廢車回收
- painter
- 小程序開發(fā)平臺(tái)前十名
- 云南軟件開發(fā)
- 網(wǎng)站建設(shè)專家
- 人口普查小程序
- 網(wǎng)站制作
- 汽車報(bào)廢管理系統(tǒng)
- 前端開發(fā)
- 云南做軟件
- 霸屏推廣
- 小程序制作
- 搜索引擎優(yōu)化
- 云南網(wǎng)絡(luò)推廣
- 百度小程序開發(fā)
- 云南網(wǎng)站建設(shè)費(fèi)用
- 云南網(wǎng)站建設(shè)列表網(wǎng)
- 云南網(wǎng)站開發(fā)
- 汽車拆解管理系統(tǒng)
- 汽車報(bào)廢系統(tǒng)
- 智慧農(nóng)貿(mào)市場(chǎng)
- 網(wǎng)絡(luò)公司排名
- 微信小程序