首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

往数据库插入数据出错了,高手帮忙,该如何解决

2012-01-06 
往数据库插入数据出错了,高手帮忙java.sql.SQLException:[Microsoft][SQLServer2000DriverforJDBC][SQLSer

往数据库插入数据出错了,高手帮忙
java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]必须声明变量   '@P1@P2@P3 '。
at   com.microsoft.jdbc.base.BaseExceptions.createException(Unknown   Source)
at   com.microsoft.jdbc.base.BaseExceptions.getException(Unknown   Source)
at   com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown   Source)
at   com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown   Source)
at   com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown   Source)
at   com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown   Source)
at   com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown   Source)
at   com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown   Source)
at   com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown   Source)
at   com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown   Source)
at   com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown   Source)
at   com.microsoft.jdbc.base.BaseStatement.executeUpdate(Unknown   Source)
at   javaBean.DB_User.add(DB_User.java:38)
at   servlet.RegServlet.doPost(RegServlet.java:127)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at   org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at   org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at   org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at   org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at   java.lang.Thread.run(Unknown   Source)
提示出错的方法:
public   class   DB_User{
//创建公用数据库连接类对象
DB_Conn   db_conn=new   DB_Conn();
//添加注册用户
public   int   add(User   user){
db_conn.ConnectDB();
try   {
String   sql   =   "insert   into   用户表(name "+ ",password,mail,trueName, "+ "sex,pQuestion,pAnswer, "+ "phone,selfintro) "+ "values( ' "   +   user.getName()
+   " ', ' "   +   user.getPassword()   +   " ', ' "   +   user.getMail()  
+   " ', ' "   +   user.getTrueName()   +   " ', ' "     +   user.getSex()    
+   " ', ' "     +   user.getPquestion()   +   " ', ' "   +   user.getPanswer()


+ " ', ' "     +   user.getPhone()+ " ', ' "   +   user.getSelfintro()+ " ') ";
//System.out.println(sql);
sql   =   new   String(sql.getBytes( "ISO8859-1 "),   "GB2312 ");
//System.out.println(sql);
int   i=db_conn.sm.executeUpdate(sql);           //   执行sql语句
return   i;
}  
catch   (SQLException   SqlE)   {
SqlE.printStackTrace();
return   -1;
}  
catch   (Exception   E)   {
E.printStackTrace();
return   -2;
}  
finally   {
//   关闭连接,释放数据库资源:
db_conn.CloseDB();

}
}


[解决办法]
送你个刚写的 我现在看代码头疼 不好意思

package bookstore;
import java.sql.*;
import java.util.*;
public class datebase
{
private Connection con=null;
private Statement sta=null;
private ResultSet rs=null;
private ResultSetMetaData resultmeta=null;
/*
初始化数据库的连接
*/
public void init()
{
String url= "jdbc:mysql://localhost/nachuan?user=root&password=1983928 ";
try
{
Class.forName( "org.gjt.mm.mysql.Driver ");
con=DriverManager.getConnection(url);
//System.out.println( "connect successfully ");
}
catch(Exception e)
{
System.out.println( "connection error has happened: "+e.getMessage());
}

}
/*
* 查询结果集 ,以键值对的形式存储于HashMap中,一条记录对应于
* 一个HashMap 存于ArrayList中
* */
public ArrayList <HashMap <String,Object> > resultset(String sql)
{
ArrayList <HashMap <String,Object> > arraylist=new ArrayList <HashMap <String,Object> > ();
try
{
init();
sta=con.createStatement();
rs=sta.executeQuery(sql);
while(rs.next())
{
HashMap <String,Object> map=new HashMap <String,Object> ();
resultmeta=rs.getMetaData();
int len=resultmeta.getColumnCount();
for(int i=1;i <len+1;i++)
{
String name=resultmeta.getColumnName(i);
Object object=rs.getObject(name);
map.put(name, object);
}
arraylist.add(map);
}
}
catch(Exception e)
{
System.out.println( "get message error: "+e.getMessage());
e.printStackTrace();
}
finally
{
try
{
destroy();
}
catch(Exception e)
{
System.out.println( "释放资源出错: "+e.getMessage());
}
}
return arraylist;
}
/*
* 销毁数据库的连接 ,释放资源
* */
public void destroy()
{
try
{ if(rs!=null)
rs.close();
rs=null;
if(sta!=null)
sta.close();
sta=null;
if(con!=null)
con.close();
con=null;
}
catch(Exception e)
{
System.out.println( "close error: "+e.getMessage());
}

}
/*
* 进行数据的添加操作 ,返回int型
* */
public int update(String sql)
{
int len=0;
try
{
init();
sta=con.createStatement();
len=sta.executeUpdate(sql);
}
catch(Exception e)
{
System.out.println( "update error happened: "+e.getMessage());


}
finally
{
try
{
destroy();
}
catch(Exception e)
{
System.out.println( "释放资源出错: "+e.getMessage());
}
}
return len;

}
/*
* 执行删除操作 返回布尔类型
* */
public boolean delete(String sql)
{
boolean bool=false;
try
{
init();
sta=con.createStatement();
sta.execute(sql);
bool=true;
}
catch(Exception e)
{
System.out.println( "delete error happened: "+e.getMessage());
bool=false;
}
finally
{
try
{
destroy();
}
catch(Exception e)
{
System.out.println( "释放资源出错: "+e.getMessage());
}
}
return bool;
}
}

[解决办法]
学习下
[解决办法]
这样的编程!太危险了!一定要多学习!

热点排行