java查询mysql装载bean_jsp与javabean链接mysql数据库并查询数据表的简单实例源码
jsp與javabean鏈接mysql數(shù)據(jù)庫并查詢數(shù)據(jù)表的簡單實例源碼。這個簡單的實例是給新手學(xué)習(xí)的,或者一些高手臨時忘記怎么使用jsp操作mysql數(shù)據(jù)庫時候查找的,包括了建立mysql數(shù)據(jù)庫連接的java文件,以及顯示查詢結(jié)果的jsp文件源碼等。希望可以給您帶來幫助。
在開發(fā)jsp項目前請建立mysql數(shù)據(jù)庫,
先創(chuàng)建數(shù)據(jù)庫:
CREATE?DATABASE?SCUTCS;
接著,創(chuàng)建表:
CREATE?TABLE?STUDENT
(
SNO?CHAR(7)?NOT?NULL,
SNAME?VARCHAR(8)?NOT?NULL,
SEX?CHAR(2)?NOT?NULL,
BDATE?DATE?NOT?NULL,
HEIGHT?DEC(5,2)?DEFAULT?000.00,
PRIMARY?KEY(SNO)
);
然后插入數(shù)據(jù),可以用SQL語句insert into values (value1, value2, …);
javabean源碼,該java是用于建立mysql數(shù)據(jù)庫連接以及查詢更改等操作,在建立項目的時候不要忘記添加相關(guān)的mysql數(shù)據(jù)庫的jar文件包:
package com.core;
import java.sql.*;
public class ConnDB{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String url = “jdbc:mysql://127.0.0.1:3306/xuexi2014″;
// MySQL配置時的用戶名
String user = “root”;
// Java連接MySQL配置時的密碼
String password = “quan123″;
public ConnDB(){
try{
Class.forName(“com.mysql.jdbc.Driver”);
}catch(java.lang.ClassNotFoundException e){
System.err.println(“連接時出錯:”+e.getMessage());
}
}
/***************************************************
*method name: executeQuery()
*功能:執(zhí)行查詢操作
*return value: ResultSet
*2014.1.18
****************************************************/
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(“查詢時的錯誤信息:”+ex.getMessage());
}finally{}
return rs;
}
/***************************************************
*method name: executeUpdate()
*功能:執(zhí)行更新操作
*return value: int
*2014-1-05
****************************************************/
public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
}finally{}
return result;
}
/***************************************************
*method name: close()
*功能:關(guān)閉數(shù)據(jù)庫鏈接
*return value: void
*2005-12-05
****************************************************/
public void close(){
try {
if (rs != null) rs.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
try {
if (stmt != null) stmt.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
}
}
顯示查詢結(jié)果的jsp文件:
ConnDB conn=new ConnDB();
ResultSet rs=conn.executeQuery(“select * from student order by SNO Desc”);
int ID=0;
String goodsName=”";
String introduce=”";
String newgoods=”";
String sale=”";
%>
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>
-//W3C//DTD HTML 4.01 Transitional//EN”>
”>
My JSP ‘MyJsp.jsp’ starting page–>
| 編號 | 姓名 | 性別 | 是否 |
try{
while(rs.next()){
ID=rs.getInt(“SNO”);
goodsName=rs.getString(“SNAME”);
introduce=rs.getString(“SEX”);
sale=rs.getInt(“sale”)==0 ? “否”:”是”;
%>
}catch(Exception e){
System.out.println(“錯誤信息:”+e.getMessage());
}
%>
總結(jié)
以上是生活随笔為你收集整理的java查询mysql装载bean_jsp与javabean链接mysql数据库并查询数据表的简单实例源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qq男生个性签名霸气
- 下一篇: mysql支不支持fulljoin_my