您的位置:首页 > 编程语言 > Java开发

java web 使用jdbc 过程错误记录

2017-04-25 21:49 357 查看
1、

HTTP Status 404 - /work_03/main.jsp%20

错误原因: %20 代表的是空格的编码,在jsp应该是名字写的有错误,后面跟了一个空格
例如:<jsp:forward  page="index.jsp " />
index.jsp后面跟了一个空格,就会出现错误


2、注意使用 if 时的括号

举例:

<%
if(name.equals("")){
String d = "用户名不能为空,重新输入";
request.setAttribute("yn", d);
%>
<jsp:forward page = "index_02.jsp" />
<%
}%>


3、 在tomcat上面建立连接池时

首先在web工程下的 META-INF 文件下面创建 context.xml 文件,里面时数据源参数配置信息
例如:


<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name = "jdbc/mysql"
type = "javax.sql.DataSource"
auth = "Container"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/students"
username = "root"
password = "123456"
maxActive = "4"
maxIdle = "2"
maxWait = "6000"
/>
</Context>


然后需要在 WEB-INF文件中的 web.xml (没有的话自己创建) 中进行配置,

<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<resource-ref>
<description>mysql数据库连接池</description>
<!--这里的名称与上面context.xml中的 name属性值一致-->
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</xml-body>


这样就可以进行连接了,建立连接的代码:

// 导入的包
<%@page import = "java.sql.*,javax.sql.*,javax.naming.*" %>

// 创建连接
Context ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();


更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web jdbc
相关文章推荐