类一:
package db;
import java.sql.*;public class DBConnection {
static { //静态块 第一次使用类时自动执行 用于加载数据库驱动
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
//用于区别不同用户连接的对象
public static ThreadLocal t = new ThreadLocal();
public static Connection getConn() throws SQLException {
Connection conn = (Connection)t.get(); //取得该用户的连接
if(conn == null||getConn().isClosed()){//第一次连接
String url="jdbc:odbc:pubs";
conn =DriverManager.getConnection(url,"sa","");
t.set(conn); //记录用户的连接
}
return conn;
}
public static void close(){ //关闭连接
try {
if (getConn() != null && !getConn().isClosed()) {
getConn().close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
类二:
package db;
import java.sql.*;public abstract class DBT { //模板 public abstract void excuteSQL() throws SQLException;//抽象方法
public void excute() throws SQLException {
Connection conn=DBConnection.getConn();
try{
conn.setAutoCommit(false); //设置自动提交为false
//执行SQL语句
excuteSQL(); conn.commit(); //提交事务
}catch(SQLException ex){
conn.rollback(); //回滚事务
ex.printStackTrace();
}finally{
conn.close(); //关闭
}
}
}
类三:
package eb;
import java.sql.*;
import java.util.ArrayList;import db.DBConnection;
import db.DBT;public class Show extends DBT {
String mname;
double mprice;
String mpicpath;
ArrayList it=new ArrayList() ;
public void excuteSQL() throws SQLException {
Connection conn=DBConnection.getConn() ;
PreparedStatement ps=conn.prepareStatement("select * from item") ;
ResultSet rs=ps.executeQuery() ;
while (rs.next() ){
Show i=new Show();
i.setMname(rs.getString("mname"));
i.setMprice(rs.getDouble("mprice") );
i.setMpicpath(rs.getString("mpicpath")) ;
it.add(i) ;
} }
public static void main(String[] args) { }
public String getMname() {
return mname;
} public String getMpicpath() {
return mpicpath;
} public ArrayList getIt() {
return it;
} public double getMprice() {
return mprice;
} public void setMpicpath(String mpicpath) {
this.mpicpath = mpicpath;
} public void setMname(String mname) {
this.mname = mname;
} public void setIt(ArrayList it) {
this.it = it;
} public void setMprice(double mprice) {
this.mprice = mprice;
}
}
最后在一个JSP页面中显示
package db;
import java.sql.*;public class DBConnection {
static { //静态块 第一次使用类时自动执行 用于加载数据库驱动
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
//用于区别不同用户连接的对象
public static ThreadLocal t = new ThreadLocal();
public static Connection getConn() throws SQLException {
Connection conn = (Connection)t.get(); //取得该用户的连接
if(conn == null||getConn().isClosed()){//第一次连接
String url="jdbc:odbc:pubs";
conn =DriverManager.getConnection(url,"sa","");
t.set(conn); //记录用户的连接
}
return conn;
}
public static void close(){ //关闭连接
try {
if (getConn() != null && !getConn().isClosed()) {
getConn().close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
类二:
package db;
import java.sql.*;public abstract class DBT { //模板 public abstract void excuteSQL() throws SQLException;//抽象方法
public void excute() throws SQLException {
Connection conn=DBConnection.getConn();
try{
conn.setAutoCommit(false); //设置自动提交为false
//执行SQL语句
excuteSQL(); conn.commit(); //提交事务
}catch(SQLException ex){
conn.rollback(); //回滚事务
ex.printStackTrace();
}finally{
conn.close(); //关闭
}
}
}
类三:
package eb;
import java.sql.*;
import java.util.ArrayList;import db.DBConnection;
import db.DBT;public class Show extends DBT {
String mname;
double mprice;
String mpicpath;
ArrayList it=new ArrayList() ;
public void excuteSQL() throws SQLException {
Connection conn=DBConnection.getConn() ;
PreparedStatement ps=conn.prepareStatement("select * from item") ;
ResultSet rs=ps.executeQuery() ;
while (rs.next() ){
Show i=new Show();
i.setMname(rs.getString("mname"));
i.setMprice(rs.getDouble("mprice") );
i.setMpicpath(rs.getString("mpicpath")) ;
it.add(i) ;
} }
public static void main(String[] args) { }
public String getMname() {
return mname;
} public String getMpicpath() {
return mpicpath;
} public ArrayList getIt() {
return it;
} public double getMprice() {
return mprice;
} public void setMpicpath(String mpicpath) {
this.mpicpath = mpicpath;
} public void setMname(String mname) {
this.mname = mname;
} public void setIt(ArrayList it) {
this.it = it;
} public void setMprice(double mprice) {
this.mprice = mprice;
}
}
最后在一个JSP页面中显示
不知道你问题出在哪,你没有把数据放到request里边啊,你怎么把数据传到页面给它显示呢.....
conn.setAutoCommit(false); //设置自动提交为false
//执行SQL语句
excuteSQL(); conn.commit(); //提交事务
这里是执行了,那个结果ArrayList没有保存到request里边让它传到页面去显示吧.
excuteSQL(); 这里应该返回那个ArrayList啊....而不是void....
<%
java.util.ArrayList list=it.getIt();
System.out.println(list.size());
for(int i=0;i<list.size();i++){
Show s=(Show)list.get(i);。
来实现的,不用request。
我是新手,有太多东西不是很明白,麻烦各位高手将错误之处,用详细点、浅显点的语言告知,谢谢
java.util.ArrayList list=it.getIt();
System.out.println(list.size());
for(int i=0;i<list.size();i++){
Show s=(Show)list.get(i);。这个里面不是有System.out.println(list.size()); 输出的多少啊?
<jsp:useBean id="it" class="eb.Show" scope="session"/>
<%
java.util.ArrayList list=it.getIt();
System.out.println(list.size());
for(int i=0;i<list.size();i++){
Show s=(Show)list.get(i);
%>
目前的问题是:没连上数据库。问题应该就在以上三段代码中。
System.out.println(list.size()); 的结果是0
我觉的那样简单一点你如果一定要用JDBC桥做的话
你的代码 可以在简单一点
类一:
package db;
import java.sql.*;public class DBConnection
{
Connection con=null;
String url; public Connection getConn(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc:odbc:pubs";
con=DriverManager.getConnection(url,"sa","");
String strSql="select * from item;
Statement stmt=con.createStatement();
//好像你这里的关键字不一样
// 你的是prepareStatement
ResultSet rs=stmt.executeQuery(strSql);
while (rs.next() ){
System.out.println(rs.getString("mname"));
System.out.println(rs.getDouble("mprice") );
System.out.println(rs.getString("mpicpath")) ;
//先看看能不能输出数据能输出的话就是后面的程序有问题
} }catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return con;
}
}
还有MVC模式像你这样做的话太麻烦了
在连接数据库的时候创建一个类
每个表的操作创建一个类
然后直接在JSP 页面调用就可以了
注意的是
CLASS=“”里面“是包名.类名”该类是数据库连接的那个
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="sqlb" class="db.DBConnection" scope="session" />
<%!Connection con=null; %>........
<td><select name="firstlevel">
<%
try {
if(sqlb.getCon()!=null){
con=sqlb.getCon();
String strSql="select * from item";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(strSql);
while(rs.next()){%>
<option><%=rs.getString(2) %></option>
<%} %>
<%}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
%>
</select>我的电脑没安装编辑器,可能有错误
但是思路应该不会有错。
要是还有问题的话,加QQ148367396
大家一起探讨。
之
“你的jsp页面里根本没有执行excute()代码啊 哪来的数据啊”
是正解,只是因为我是新手,今天下午才领悟到,呵呵,现在问题解决了。
感谢众热心高手的耐心指点啊。