初学大家帮我看看错:jsp操作数据库
login.jsp:
<%@page contentType= "text/html;charset=gb2312 "%>
<html>
<head>
<title> 数据库登陆
</title>
</head>
<body>
<center>
<h1> 登陆 </h1>
<hr>
<br>
<br>
<table>
<form action= "conn.jsp " method= "post ">
<tr>
<td>
数据库名
</td>
<td>
<input type= "text " name= "dbname ">
</td>
</tr>
<tr>
<td>
用户名
</td>
<td>
<input type= "text " name= "uname ">
</td>
</tr>
<tr>
<td>
密 码
</td>
<td>
<input type= "password " name= "upassword ">
</td>
</tr>
<tr>
<td>
表 名
</td>
<td>
<input type= "text " name= "tablename ">
</td>
</tr>
<tr>
<td>
<input type= "submit " value= "确定 ">
</td>
<td>
<input type= "reset " value= "重置 ">
</td>
</tr>
</table>
</center>
</body>
</html>
conn.jsp:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@page import= "java.sql.* "%>
<%@page import= "java.io.* "%>
<%!
String Driverpro= "com.mysql.jdbc.Driver ";
static Connection con=null;
%>
<%
String username=request.getParameter( "uname ");
String dbname=request.getParameter( "dbname ");
String password=request.getParameter( "upassword ");
String tablename=request.getParameter( "tablename ");
//out.println(tablename);
String url= "jdbc:mysql://localhost/ "+dbname;
%>
<%
try{
Class.forName(Driverpro);
con=DriverManager.getConnection(url,username,password);
//con.setCatalog(dbname);
}catch(Exception e)
{
out.println( "连接失败 ");
}
%>
<%//这一段是想把输入的东西存到一个文件中
String nameOfTextFile = "d:/databaseconf.txt ";
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(username);
pw.println(password);
pw.println(dbname);
pw.close();
} catch(IOException e) {
out.println(e.getMessage());
}
%>
<%
request.setAttribute( "tablename ",tablename);
request.setAttribute( "con ",con);
//request.setAttribute( "test1 ", "conn ");//测试
%>
<jsp:forward page= "cmd.jsp "/>
cmd.jsp:
<%@ page contentType= "text/html; charset=gb2312 "%>
<%@page import= "java.sql.* "%>
<html>
<head>
<title> 数据库查询结果
</title>
</head>
<body>
<center>
<table border= "1 ">
<%
//Connection conn=(Connection)application.getAttribute( "con ");
String tablename=(String)request.getAttribute( "tablename ");
Connection con=(Connection)request.getAttribute( "con ");
Statement stmt=null;
ResultSet rs=null;
//out.println(tablename);
try
{
String sqlcmd= "select * from "+tablename;
stmt=con.createStatement();
rs=stmt.executeQuery(sqlcmd);
%>
查询成功
<tr>
<td>
姓名
</td>
<td>
年龄
</td>
</tr>
<%
while(rs.next())
{
String name=rs.getString( "name ");
String age=rs.getString( "age ");
name=new String(name.getBytes( "ISO-8859-1 "), "GB2312 ");
age=new String(age.getBytes( "ISO-8859-1 "), "GB2312 ");
%>
<tr>
<td>
<%=name%>
</td>
<td>
<%=age%>
</td>
</tr>
<%
}
}catch(Exception e)
{
%>
<%
}
%>
<% // 4、关闭数据库
try
{
// 关闭操作
rs.close() ;
// 关闭操作
stmt.close() ;
// 关闭连接
//conn.close() ;
}
catch(Exception e)
{
%>
数据库关闭失败!!!
<%
}
request.setAttribute( "test ", "cmd ");//测试
%>
</table>
</center>
</body>
<a href= "addinput.jsp?conn=con&ttablename=tablename "> 添加 </a>
<a href= "login.jsp "> 结束操作 </a>
</html>
addinput.jsp
<%@page contentType= "text/html;charset=gb2312 "%>
<%out.println( "lala ");%>
<%@page import= "java.sql.* "%>
<%
Connection con=(Connection)request.getAttribute( "conn ");
String tablename=(String)request.getAttribute( "ttablename ");
request.setAttribute( "conn ",con);
request.setAttribute( "ttablename ",tablename);
%>
<html>
<head>
<title> 插入数据
</title>
</head>
<body>
<center>
<table >
<form action= "add.jsp " method= "post ">
<h1> 插入数据 </h1>
<hr>
<br>
<br>
<tr>
<td>
姓 名
</td>
<td>
<input type= "text " name= "addname ">
</td>
</tr>
<tr>
<td>
年 龄
</td>
<td>
<input type= "text " name= "addage ">
</td>
</tr>
<tr>
<td>
<input type= "submit " value= "确定 ">
</td>
<td>
<input type= "reset " value= "重置 ">
</td>
</tr>
</table>
</center>
</body>
</html>
add.jsp:
<%@page contentType= "text/html;charset=gb2312 "%>
<%@page import= "java.sql.* "%>
<jsp:forword page= "conn.jsp "/>
<%
String tablename=(String)request.getAttribute( "ttablename ");
Connection con=(Connection)request.getAttribute( "conn ");
Statement stmtt=null;
%>
<%
//connn=(Connection)application.getAttribute( "con ");
String adname=request.getParameter( "addname ");
String adage=request.getParameter( "addage ");
out.println(adname);
out.println(adage);
// String tablename=(String)application.getAttribute( "tablename ");
//String DBURL = "jdbc:mysql://localhost/ "+dbname ;
//String address= "Noname2.jsp?dbname= "+dbname+ "&uname= "+uname+ "&upassword= "+upassword+ "&tname= "+ "stu " ;
%>
<%
// 操作数据库如果要写入
// 通过Connection对象实例化Statement对象
try
{
stmtt = connn.createStatement() ;
// 为sql变量赋值
// 插入语句
sql = "INSERT INTO "+tablename+ " (name,age) VALUES ( ' "+adname+ " ', "+adage+ ") " ;
stmtt.executeUpdate(sql) ;
%>
插入数据完毕
<% }
catch(Exception e)
{
%>
升级失败!!!
<%
}
%>
<% // 4、关闭数据库
try
{
// 关闭操作
stmtt.close() ;
// 关闭操作
//stmtt.close() ;
// 关闭连接
//conn.close() ;
}
catch(Exception e)
{
%>
数据库关闭失败!!!
<%
}
%>
<a href= "cmd.jsp "> 察看结果 </a>
[解决办法]
什么错啊,这么长,看得头都晕
[解决办法]
请楼主说一下运行后出现的错误!!!
[解决办法]
如果有报错页面,麻烦楼主贴出来看看,如果没有,请说说错误的现象.
[解决办法]
发布了吗?/uuu/add.jsp路径对吗?
[解决办法]
500的错误啊?
晕了
这错误最讨厌了
先看路径吧
我敢保证 你还有别的错误
[解决办法]
路径错误,查查你写的路径就知道了
[解决办法]
<jsp:forword page= "conn.jsp "/> 此行的错误:非标准动作?
