JSP 向数据库插入内容失败,大家帮忙看看!
我的数据库连接时写了一个JavaBean------------------------------------------------packageorg.util;i...
我的数据库连接时写了一个JavaBean
------------------------------------------------
package org.util;
import java.sql.*;
public class DB {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
private static final String DRIVE = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String DBURL = "jdbc:microsoft:sqlserver://localhost:1305;DatabaseName=lianxi";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";
public DB() { //加载驱动
try {
Class.forName(DRIVE);
}
catch (Exception e) {}
}
public Connection getConnection() { //得到连接
try {
con = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
}catch (Exception e) {
con = null;
}
return con;
}
public void dropConnection() { //关闭连接
try {
closeStmt();
con.close();
} catch (Exception e) {}
finally {
con = null;
}
}
public ResultSet executeQuery(String sql) { //执行sql查询
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {}
return rs;
}
public int executeUpdate(String sql) { //执行sql更新语句
int i=0;
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
i= stmt.executeUpdate(sql);
stmt.close();
con.close();
}catch (SQLException ex) {}
return i;
}
public void execute(String sql) { //执行sql语句
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close();
}catch (SQLException ex) {}
}
public void closeConn() { //关闭sql连接
try {
stmt.close();
}
catch (SQLException e) {}
}
public void closeStmt() { //关闭sql连接
try {
con.close();
}catch (SQLException e) {}
}
}
------------------------------------------
然后插入页面时这样的
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"import="java.sql.*"%>
<%@ page import="org.util.DB" %>
<html>
<head>
<title>index2.jsp</title>
</head>
<body>
<%
DB one=new DB();
String sql = "INSERT INTO admin(username,userpass) values(hello,123456)" ;
one.executeUpdate(sql);
ResultSet rs=one.executeQuery("select * from admin");
while(rs.next()){
%>
ID:<%=rs.getString("id")%><br>
姓名:<%=rs.getString("username")%><br>
密码:<%=rs.getString("userpass")%>
<%
}
%>
</body>
</html>
现在是执行完这段代买之后,数据库里查询不出来刚刚添加的数据,大家帮忙看看是什么问题! 展开
------------------------------------------------
package org.util;
import java.sql.*;
public class DB {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
private static final String DRIVE = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String DBURL = "jdbc:microsoft:sqlserver://localhost:1305;DatabaseName=lianxi";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";
public DB() { //加载驱动
try {
Class.forName(DRIVE);
}
catch (Exception e) {}
}
public Connection getConnection() { //得到连接
try {
con = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
}catch (Exception e) {
con = null;
}
return con;
}
public void dropConnection() { //关闭连接
try {
closeStmt();
con.close();
} catch (Exception e) {}
finally {
con = null;
}
}
public ResultSet executeQuery(String sql) { //执行sql查询
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {}
return rs;
}
public int executeUpdate(String sql) { //执行sql更新语句
int i=0;
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
i= stmt.executeUpdate(sql);
stmt.close();
con.close();
}catch (SQLException ex) {}
return i;
}
public void execute(String sql) { //执行sql语句
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close();
}catch (SQLException ex) {}
}
public void closeConn() { //关闭sql连接
try {
stmt.close();
}
catch (SQLException e) {}
}
public void closeStmt() { //关闭sql连接
try {
con.close();
}catch (SQLException e) {}
}
}
------------------------------------------
然后插入页面时这样的
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"import="java.sql.*"%>
<%@ page import="org.util.DB" %>
<html>
<head>
<title>index2.jsp</title>
</head>
<body>
<%
DB one=new DB();
String sql = "INSERT INTO admin(username,userpass) values(hello,123456)" ;
one.executeUpdate(sql);
ResultSet rs=one.executeQuery("select * from admin");
while(rs.next()){
%>
ID:<%=rs.getString("id")%><br>
姓名:<%=rs.getString("username")%><br>
密码:<%=rs.getString("userpass")%>
<%
}
%>
</body>
</html>
现在是执行完这段代买之后,数据库里查询不出来刚刚添加的数据,大家帮忙看看是什么问题! 展开
展开全部
两条sql语句需要两个 Statement
<%
DB one=new DB();
DB two=new DB();
String sql = "INSERT INTO admin(username,userpass) values(hello,123456)" ;
one.executeUpdate(sql);
ResultSet rs=two.executeQuery("select * from admin");
while(rs.next()){
%>
ID:<%=rs.getString("id")%><br>
姓名:<%=rs.getString("username")%><br>
密码:<%=rs.getString("userpass")%>
<%
}
%>
试试……
不行再问我!
<%
DB one=new DB();
DB two=new DB();
String sql = "INSERT INTO admin(username,userpass) values(hello,123456)" ;
one.executeUpdate(sql);
ResultSet rs=two.executeQuery("select * from admin");
while(rs.next()){
%>
ID:<%=rs.getString("id")%><br>
姓名:<%=rs.getString("username")%><br>
密码:<%=rs.getString("userpass")%>
<%
}
%>
试试……
不行再问我!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的一系列操作都是在内存中进行的,只有commit之后,才会写到库中去。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String sql = "INSERT INTO admin(username,userpass) values(hello,123456)" ;
这句话有问题,改为("hello","123456")
这句话有问题,改为("hello","123456")
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-10-26
展开全部
没有提交 commit()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询