JSP向数据库中插入 数据出现错误,请大虾帮忙
我向数据库中插入数据
出现一下错误:
ava.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]将截断字符串或二进制数据。
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.TDSExecuteRequest.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 connDB.connectDB.executeUpdata(connectDB.java:78)
at connDB.connectDB.main(connectDB.java:173)
我在网上也查了查,说是字段长度不同,我的没右这个问题。
还有说是jdbc的三个包的问题,我也试了,把这三个包添入classpath中,但是也不行啊,还是这个问题。
我把我的java类传上来大家帮忙看看,我用的数据库是sqlserver2000
package connDB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class connectDB{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver:// ";
private final String serverName= "localhost ";
private final String portNumber = "1433 ";
private final String databaseName= "ZYXW ";
private final String userName = "sa ";
private final String password = " ";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor ";
// Constructor
public connectDB(){}
private String getConnectionUrl(){
return url+serverName+ ": "+portNumber+ ";databaseName= "+databaseName+ ";selectMethod= "+selectMethod+ "; ";
}
private java.sql.Connection getConnection(){
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println( "Connection Successful! ");
}catch(Exception e){
e.printStackTrace();
System.out.println( "Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
//读取数据
public ResultSet executeQuery(String sql) throws SQLException
{
String query = sql;
ResultSet rs = null;
try{
Connection con=getConnection();
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
}catch(Exception e)
{
e.printStackTrace();
}
return rs;
}
public int executeUpdata(String sql)throws SQLException
{
String query = sql;
int result = 0 ;
try{
Connection con = getConnection();
Statement stmt = con.createStatement();
result = stmt.executeUpdate(query);
}catch(Exception e){
e.printStackTrace();
}
return result;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public String gettime() {
String datestr = " " ;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat( "yyyy-MM-d HH:mm:ss ") ;
java.util.Date date = new java.util.Date() ;
datestr = df.format(new java.util.Date()) ;
}
catch (Exception ex) {
}
return datestr ;
}
public String ex_chinese(String str){
if(str==null){
str = " " ;
}
else{
try {
str = new String(str.getBytes( "iso-8859-1 "), "gb2312 ") ;
}
catch (Exception ex) {
}
}
return str ;
}
public static void main(String[] args) throws Exception
{
String note_type= "11111 ";
String type = "11 ";
String author= "adf ";
String title= "dafa ";
String content= "我斯蒂芬哦阿打发 ";
connectDB connectDb = new connectDB();
// 查询note_id
ResultSet rs_cx = null;
String sql_cx = "select note_id from notes_jm where note_type= ' "+note_type+ " ' order by note_id desc ";
rs_cx = connectDb.executeQuery(sql_cx);
rs_cx.next();
String note_id;
if(rs_cx.getRow()==0)
{
double double_note_id= Double.parseDouble(note_type)*1000000+1;
System.out.println(double_note_id+ "double_note_id ");
note_id = Double.toString(double_note_id);
}else
{
double double_note_id = Double.parseDouble(rs_cx.getString( "note_id "));
System.out.println(double_note_id);
note_id = Double.toString(double_note_id+1);
}
//取得发布信息的时间(为系统时间)
String year = new Date().toLocaleString().substring(0,4);
System.out.println(1);
String month ;
String date;
int intMonth = new Date().getMonth()+1;
int intdate = new Date().getDate();
month = Integer.toString(intMonth);
date = Integer.toString(intdate);
/*if(intMonth <10)
{
month = "0 "+Integer.toString(intMonth);
}else{
month = Integer.toString(intMonth);
}
if(intdate <10)
{
date = "0 "+Integer.toString(intdate);
}else{
date = Integer.toString(intdate);
}*/
String time = year+ "- "+month+ "- "+date;
//向数据库中插入数据
String file=null;
String pic = null;
String sql_insert = "insert into notes_jm(author,title,time,content,note_id,type,note_type) values( ' "+author+ " ', ' "+title+ " ', ' "+time+ " ', ' "+content+ " ', ' "+note_id+ " ', '11111 ', ' "+note_type+ " ') ";
int result = connectDb.executeUpdata(sql_insert);
if(result==0)
{
System.out.println( "失败 ");
}else{
System.out.println( "成功 ");
}
}
}
[解决办法]
数据库字段的类型和数据类型不一致
[解决办法]
调试下,一步一步走下,看停在哪不就知道是哪个字段长度出问题啦。肯定是字段长度有问题
[解决办法]
String year = new Date().toLocaleString().substring(0,4);
感觉应该是这个地方出错了,你可以debug下 很easy 的
[解决办法]
首先可以告诉搂主你连数据库肯定没问题了!呵呵!
把你的sql语句输出一下!用out.println()或System.out.println(),就知道哪错了!
[解决办法]
你的数据库的字段类型和你的语句中的数据类型不一致,要么是长度,要么是类型,前者的可能性大一些,因为我也出现过,比如字段长度定义 50 ,你的数据超过了,就会出问题。
你可以把 SQL 语句打印出来
然后用查询分析器做一次
把对应的字段改过来,不要那么小气,所有字符串的字段都可以设置为 varchar 500 的
呵呵!!!
[解决办法]
顶楼上
[解决办法]
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]将截断字符串或二进制数据。
数据库的字段长度设置少了,你插入的数据大于你设置的长度.