当前位置: 技术问答>java相关
从数据库取值的问题????
来源: 互联网 发布时间:2015-06-21
本文导语: 我的连接数据库bean如下: package com.db; import java.sql.*; import java.util.*; import javax.sql.*; import javax.naming.*; import java.io.*; public class Books { String error; Connection con; public Books() { } public void connect() throw...
我的连接数据库bean如下:
package com.db;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
import java.io.*;
public class Books {
String error;
Connection con;
public Books() { }
public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
InitialContext ctx =new InitialContext();
DataSource ds =(javax.sql.DataSource)ctx.lookup("oracle");
con=ds.getConnection();
}
catch(Exception e){
error = ("SQLException: Unable to connection database.");
throw new SQLException(error);
}
}
public void disconnect() throws SQLException {
try {
if ( con != null ) {
con.close();
}
} catch (SQLException sqle) {
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}
public ResultSet viewBooks(String queryString) throws SQLException, Exception {
ResultSet rs = null;
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute the query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while retrieving books.";
throw new Exception(error);
}
return rs;
}
}
jsp页面如下:
query_menu
A:link {
COLOR: #FFCC33; TEXT-DECORATION: none
}
A:visited {
COLOR: #ff6633; TEXT-DECORATION: none
}
A:hover {
COLOR: #339933; TEXT-DECORATION: underline
}
A:active {
COLOR: #990099
}
.TgSpToc1ItemFolderNoLink {
COLOR: #FFFFFF; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
.TgSpToc1ItemFolderLink {
COLOR: black; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
.TgSpToc1ItemNoLink {
COLOR: #FFFFFF; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: bold
}
.TgSpToc1ItemLink {
COLOR: #FFCC33; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
var myToc1 = new TgSpToc_toc('myToc1');
myToc1.styleItemLink ='TgSpToc1ItemLink';
myToc1.styleItemNoLink ='TgSpToc1ItemNoLink';
myToc1.styleItemFolderLink ='TgSpToc1ItemFolderLink';
myToc1.styleItemFolderNoLink='TgSpToc1ItemFolderNoLink';
myToc1.showRoot = true;
myToc1.showIcons = true;
myToc1.showTextLinks = true;
myToc1.iconPath = 'images/';
myToc1.iconWidth = '24';
myToc1.iconHeight = '22';
myToc1.iconEmpty = 'images/Line2.gif';
myToc1.iconPlus = 'images/Plus.gif';
myToc1.iconPlus1 = 'images/Plus2.gif';
myToc1.iconPlus2 = 'images/Plus2.gif';
myToc1.iconMinus = 'images/Minus.gif';
myToc1.iconMinus1 = 'images/Minus2.gif';
myToc1.iconMinus2 = 'images/Minus2.gif';
myToc1.iconLine1 = 'images/Line2.gif';
myToc1.iconLine2 = 'images/Line2.gif';
myToc1.iconLine3 = 'images/Line3.gif';
myToc1.iconItem = 'images/Item.gif';
myToc1.iconFolderExpanded = 'images/FolderExp.gif';
myToc1.iconFolderCollapsed = 'images/FolderCol.gif';
myToc1_node1= myToc1.makeFolder('','','MAIN','');
package com.db;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
import java.io.*;
public class Books {
String error;
Connection con;
public Books() { }
public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
InitialContext ctx =new InitialContext();
DataSource ds =(javax.sql.DataSource)ctx.lookup("oracle");
con=ds.getConnection();
}
catch(Exception e){
error = ("SQLException: Unable to connection database.");
throw new SQLException(error);
}
}
public void disconnect() throws SQLException {
try {
if ( con != null ) {
con.close();
}
} catch (SQLException sqle) {
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}
public ResultSet viewBooks(String queryString) throws SQLException, Exception {
ResultSet rs = null;
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute the query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while retrieving books.";
throw new Exception(error);
}
return rs;
}
}
jsp页面如下:
query_menu
A:link {
COLOR: #FFCC33; TEXT-DECORATION: none
}
A:visited {
COLOR: #ff6633; TEXT-DECORATION: none
}
A:hover {
COLOR: #339933; TEXT-DECORATION: underline
}
A:active {
COLOR: #990099
}
.TgSpToc1ItemFolderNoLink {
COLOR: #FFFFFF; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
.TgSpToc1ItemFolderLink {
COLOR: black; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
.TgSpToc1ItemNoLink {
COLOR: #FFFFFF; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: bold
}
.TgSpToc1ItemLink {
COLOR: #FFCC33; FONT-FAMILY: "宋体"; FONT-SIZE: 14px; FONT-WEIGHT: normal; TEXT-DECORATION: none
}
var myToc1 = new TgSpToc_toc('myToc1');
myToc1.styleItemLink ='TgSpToc1ItemLink';
myToc1.styleItemNoLink ='TgSpToc1ItemNoLink';
myToc1.styleItemFolderLink ='TgSpToc1ItemFolderLink';
myToc1.styleItemFolderNoLink='TgSpToc1ItemFolderNoLink';
myToc1.showRoot = true;
myToc1.showIcons = true;
myToc1.showTextLinks = true;
myToc1.iconPath = 'images/';
myToc1.iconWidth = '24';
myToc1.iconHeight = '22';
myToc1.iconEmpty = 'images/Line2.gif';
myToc1.iconPlus = 'images/Plus.gif';
myToc1.iconPlus1 = 'images/Plus2.gif';
myToc1.iconPlus2 = 'images/Plus2.gif';
myToc1.iconMinus = 'images/Minus.gif';
myToc1.iconMinus1 = 'images/Minus2.gif';
myToc1.iconMinus2 = 'images/Minus2.gif';
myToc1.iconLine1 = 'images/Line2.gif';
myToc1.iconLine2 = 'images/Line2.gif';
myToc1.iconLine3 = 'images/Line3.gif';
myToc1.iconItem = 'images/Item.gif';
myToc1.iconFolderExpanded = 'images/FolderExp.gif';
myToc1.iconFolderCollapsed = 'images/FolderCol.gif';
myToc1_node1= myToc1.makeFolder('','','MAIN','');