tomcat 没有报错,但是请求后的网页上的内容是:
the Wrong about Driver
the Wrong about SQL
说明我的,加载驱动有问下周,数据为连接也有问题?问题出在哪儿?我自己他细看,好像没有问题
<%@page contentType="text/html;charset=GBK" import="java.sql.*" %>
<html>
<body bgcolor="pink">
<% Connection conn;
String DataBaseDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DataBaseName="jdbc:microsoft:sqlserver;DatabaseName=StudenttData";
try
{
Class.forName(DataBaseDriver);
}
catch(ClassNotFoundException ex)
{
out.println("the Wrong about Driver"+"<br>");
}
try
{
conn=DriverManager.getConnection(DataBaseName,"sa","");
Statement stmt=conn.createStatement();
String sql="select * from Score where StudentNumber=2";
//PreparedStatement pstmt =conn.prepareStatement(sql);
// pstmt.setString(1,"2");
//ResultSet rs=pstmt.executeQuery();
ResultSet rs=stmt.executeQuery(sql);
out.println("StudentNumber: "+rs.getString("StudnetNumber"));
out.println("Name: "+rs.getString("Name"));
out.println("Chinese: "+rs.getString("Chinese"));
out.println("English: "+rs.getString("English"));
out.println("Math: "+rs.getString("Math"));
}
catch(SQLException se)
{
out.print("the Wrong about SQL");
}
%>
</body>
</html>
the Wrong about Driver
the Wrong about SQL
说明我的,加载驱动有问下周,数据为连接也有问题?问题出在哪儿?我自己他细看,好像没有问题
<%@page contentType="text/html;charset=GBK" import="java.sql.*" %>
<html>
<body bgcolor="pink">
<% Connection conn;
String DataBaseDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DataBaseName="jdbc:microsoft:sqlserver;DatabaseName=StudenttData";
try
{
Class.forName(DataBaseDriver);
}
catch(ClassNotFoundException ex)
{
out.println("the Wrong about Driver"+"<br>");
}
try
{
conn=DriverManager.getConnection(DataBaseName,"sa","");
Statement stmt=conn.createStatement();
String sql="select * from Score where StudentNumber=2";
//PreparedStatement pstmt =conn.prepareStatement(sql);
// pstmt.setString(1,"2");
//ResultSet rs=pstmt.executeQuery();
ResultSet rs=stmt.executeQuery(sql);
out.println("StudentNumber: "+rs.getString("StudnetNumber"));
out.println("Name: "+rs.getString("Name"));
out.println("Chinese: "+rs.getString("Chinese"));
out.println("English: "+rs.getString("English"));
out.println("Math: "+rs.getString("Math"));
}
catch(SQLException se)
{
out.print("the Wrong about SQL");
}
%>
</body>
</html>
应该改为:
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=StudenttData
1.将数据库驱动程序的JAR文件放在Tomcat的 common/lib 中;
2.在server.xml中设置数据源,以MySQL数据库为例,如下:
在<GlobalNamingResources> </GlobalNamingResources>节点中加入,
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/test"
maxActive="4"/>
属性说明:name,数据源名称,通常取”jdbc/XXX”的格式;
type,”javax.sql.DataSource”;
password,数据库用户密码;
driveClassName,数据库驱动;
maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
接将被标记为不可用,然后被释放。设为0表示无限制。
MaxActive,连接池的最大数据库连接数。设为0表示无限制。
maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示
无限制。
3.在你的web应用程序的web.xml中设置数据源参考,如下:
在<web-app></web-app>节点中加入,
<resource-ref>
<description>MySQL DB Connection Pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
子节点说明: description,描述信息;
res-ref-name,参考数据源名字,同上一步的属性name;
res-type,资源类型,”javax.sql.DataSource”;
res-auth,”Container”;
res-sharing-scope,”Shareable”;
4.在web应用程序的context.xml中设置数据源链接,如下:
在<Context></Context>节点中加入,
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
属性说明:name,同第2步和第3步的属性name值,和子节点res-ref-name值;
type,同样取”javax.sql.DataSource”;
global,同name值。 至此,设置完成,下面是如何使用数据库连接池。
1.建立一个连接池类,DBPool.java,用来创建连接池,代码如下:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource; public class DBPool {
private static DataSource pool;
static {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
}
public static DataSource getPool() {
return pool;
}
}
http://ncujjq.blog.sohu.com/47483006.html