jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'tianjia.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
    <form action="<%=path%>/servlet/Tianjia" method="post" name="form1"><table width="822">
  <tr>
    <td colspan="3"><div align="center">图书添加</div></td>
    </tr>
  <tr>
    <td width="236">&nbsp;</td>
    <td width="287"><div align="right">图书编号: 
      <input name="id" type="text" id="id" />
    </div></td>
    <td width="283">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">书名: 
      <input name="name" type="text" id="name" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">作者: 
      <input name="author" type="text" id="author" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">出版社: 
      <input name="publisher" type="text" id="publisher" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">价格: 
      <input name="price" type="text" id="price" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">日期: 
      <input name="date" type="text" id="date" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">图书类别: 
      <input name="category" type="text" id="category" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">图书数量: 
      <input name="num" type="text" id="num" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><div align="right">图书内容: 
      <input name="content" type="text" id="content" />
    </div></td>
    <td>&nbsp;</td>
  </tr>
  
  <tr>
    <td>&nbsp;</td> 
     
    <td>
      <div align="center">
  <input type="submit" name="Submit" value="提交" />
  &nbsp;&nbsp;
  <input type="reset" name="Submit2" value="重置" />
        </div></td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>下面是Tianjia.java
package servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import bookp.Datebase;
public class Tianjia extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk"); //设定输出到客户浏览器的编码类型
request.setCharacterEncoding("GBK");  //设定传入参数的字符编码。
HttpSession session=request.getSession();
Datebase db=new Datebase();
String sql;
String sql2;
boolean bo1;
boolean bo2;
String information;
String id=request.getParameter("id");  //获取表单传入的参数
String name=request.getParameter("name");
String author=request.getParameter("author");
String publisher=request.getParameter("publisher");
String date=request.getParameter("date");
String price=request.getParameter("price");
String category=request.getParameter("category");
String num=request.getParameter("num");
String content=request.getParameter("content");

sql="insert into booklib(id,name,author,publisher,date,price,category,num,content) values ("+"'"+id+"','"+name+"','"+author+"','"+publisher+"','"+date+"','"+price+"','"+category+"','"+num+"','"+content+")";
sql2="select * from booklib where id="+"'"+id+"'";
bo2=db.queryboolen(sql2);

if(bo2){
information="对不起,id已被使用,请换一个!";
db.close();
session.setAttribute("information",information);  
//request.setAttribute("information",information);  
//request.getRequestDispatcher("/result.jsp").forward(request, response);
response.sendRedirect(request.getContextPath()+"/tianjia.jsp"); 
}
else{
bo1=db.exeSql(sql);

if(bo1){

information="添加成功!";
session.setAttribute("infomation",information); 
response.sendRedirect(request.getContextPath()+"/tianjiasuccess.jsp"); 
}
else{
db.close();
information="添加失败!请重新添加";
session.setAttribute("infomation",information); 
response.sendRedirect(request.getContextPath()+"/tianjia.jsp"); 
}
}
}
}
数据库连接
package bookp;import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;public class Datebase {
String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 连接sql数据库的方法
String url = "";
String username = "";
String password = "";
Connection dbcon=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement statement=null;
private static String FILE_PATH_NAME = "/database.properties";//数据库路径名 /*
 * 初始化操作,包括给变量赋初值和连接数据库
 */
public Datebase() {
try {
        InputStream in = getClass().getResourceAsStream(FILE_PATH_NAME);
        Properties props = new Properties();
        props.load(in);
        in.close();
         System.out.println(getClass().getResourceAsStream(FILE_PATH_NAME));
        username = props.getProperty("username");
        password = props.getProperty("password");
        url = "jdbc:sqlserver://localhost:" + props.getProperty("port")
              + ";DatabaseName=library";
        Class.forName(dbDriver); // 声明所调用的类包
        dbcon = DriverManager.getConnection(url, username, password); // 获得数据库的连接对象
      } catch (Exception e) {
        e.printStackTrace();
      }
}

/*
 * 对数据库执行插入、删除、更新 等操作,返回一个布尔值。
 */
public boolean exeSql(String strSql) {
try {
stmt = dbcon.createStatement();  //由Connection对象创建Statement对象。
stmt.executeUpdate(strSql);    //可以返回受影响的行数。
return true;
} catch (Exception e) {
System.out.println(e.toString());
return false;
}
}
/*
 * 执行查询操作,返回一个ResultSet类型的对象。
 */
public ResultSet exeSqlQuery(String strSql) {
try {
stmt = dbcon.createStatement();
rs = stmt.executeQuery(strSql);  //返回单个ResultSet对象即查询结果集。
} catch (SQLException e) {
System.out.println(e.toString());
rs = null;
}
return rs;
}

/*
*执行查询,返回boolean型。
*/
public boolean queryboolen(String strSql){
boolean bb=false;
try{

stmt=dbcon.createStatement();
rs=stmt.executeQuery(strSql);
if(rs.next()){
bb=true;
}
}catch(SQLException e){
System.out.println(e.toString());
bb=false;
}
return bb;

}
/*
 * 使用预编译语句,由Connection对象创建PrepareStatement对象。
 * 然后执行sql语句中的每一个问号【从1开始编号】,比如statement.setString(1,username);
 * 然后执行ResultSet rest=statement.executeQuery() 来执行sql语句;即返回的是ResultSet查询结果集,
 * 然后可用while(rest.next())来列出,比如rest。getString("name")等。
 * 如果查询语句没有问号的话直接执行PrepareStatement对象statement的executeQuery()返回ResultSet结果集。
 * 
 * 如果是插入语句也是先设置预处理语句参数即问号。然后执行statement.executeUpdate()执行sql语句。
 */
public PreparedStatement exeprepared(String strSql){

try {
statement=dbcon.prepareStatement(strSql); //只是返回PrepareStatement对象,不是结果集。
} catch (SQLException e) { e.printStackTrace();
}
return statement;
}

public int get_number(String sql){
int num=0;
     try{

stmt=dbcon.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
num=rs.getInt("countnum");
}
rs.close();
}catch(SQLException e){
System.out.println(e.toString());
}
return num;
} /*
 *关闭数据库连接
 */
 public void close()
 {
  try{
  if(rs!=null)
  rs.close();
  if(stmt!=null)
        stmt.close();
  if(dbcon!=null)
        dbcon.close();
  }catch(Exception e)
     {
  System.out.println(e.getMessage());
  }
  }    
    public static void main(String[] args) {
     ResultSet rs1;
     Datebase db=new Datebase();
     rs1=db.exeSqlQuery("select * from userinfo");
     try {
while(rs1.next())
{
System.out.println(rs1.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
db.close();       
    }} 为什么执行完  数据库中没有数据呢

解决方案 »

  1.   

    我滴个神。你先debug找sql语句,然后数据库里执行以下,看有没有问题啊。
    sql="insert into booklib(id,name,author,publisher,date,price,category,num,content) values ("+"'"+id+"','"+name+"','"+author+"','"+publisher+"','"+date+"','"+price+"','"+category+"','"+num+"','"+co...
    你sql语句写的不对。("+"'"+id+"', 这个什么意思。
      

  2.   

    这是我用的师兄的写的代码形式    就是上面获得的各个参数值、那怎么写  
    sql="insert into booklib(id,name,author,publisher,date,price,category,num,content)values(id,name,author,publisher,date,price,category,num,content)";可以吗
      

  3.   

    你的sql的值打印出来,看是否正确
      

  4.   

    拼字符一定要仔细,转到jsp区吧