Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MS SQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 数据插入数据库的问题,新手急用!
youyou714





发贴: 3
积分: 0
于 2006-07-03 13:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sql server 2000数据库中的一个表的主键设置成了标识,怎么向这个表中插入数据啊


作者 Re:数据插入数据库的问题,新手急用! [Re:youyou714]
elephi





发贴: 45
积分: 60
于 2006-07-03 13:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果你是直接用SQL插入,那就在insert中插入你需要的列,除了主键除外.SQLSERVER应当自动生成标识主键


作者 Re:数据插入数据库的问题,新手急用! [Re:youyou714]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-07-03 21:29 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
If you want to manipulate the identity column, you could turn off the identity column temporarily with SET_IDENTITY_INSERT


作者 Re:数据插入数据库的问题,新手急用! [Re:elephi]
youyou714





发贴: 3
积分: 0
于 2006-07-04 17:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Mytest1
{
public static void main(String args[])
{
Connection con;
Statement stmt=null;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}
catch(ClassNotFoundException e)
{
System.out.println("lian jie bu cheng gong");
}
try
{
con=DriverManager.getConnection

("jdbc:odbc:mybase","sa","");
stmt=con.createStatement();
rs=stmt.executeQuery("insert into test values

('sood','weowjf')");


stmt.close();
con.close();
}
catch(SQLException e)
{
System.out.println("cao zuo shi bai!");
}

}
}
大哥们继续帮我看看这段代码怎么就不能把数据写入数据库了
谢谢两位的指点



作者 Re:数据插入数据库的问题,新手急用! [Re:youyou714]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-07-04 20:01 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
catch(SQLException e)
{
// dispaly the error stack rather than this unhelpful message.
//System.out.println("cao zuo shi bai!");
e.printStackTrace();
}

// also for the other one, ClassNotFoundException e


>rs=stmt.executeQuery("insert into test values
>
>('sood','weowjf')");
??? is it one line? And
rs=stmt.executeQuery("insert into test values('sood','weowjf')");
should be
stmt.executeUpdate("insert into test values('sood','weowjf')");



作者 Re:数据插入数据库的问题,新手急用! [Re:youyou714]
yclc





发贴: 37
积分: 1
于 2006-07-08 14:52 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果你含有IDENTITY(1,1)类型主键,插入时请在你表名后面加上除了主键外的字段列表
eg.insert into test(column1,column2) values(values1,values2)

make sure both column1 and column2 are all not primary key which is identity



作者 Re:数据插入数据库的问题,新手急用! [Re:yclc]
youyou714





发贴: 3
积分: 0
于 2006-07-09 18:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢各位大哥的帮助,问题已经解决



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923