有关数据库的问题,有高手帮忙执行一下有什么错误啊。。

linyong07 2007-06-14 12:51:48
import java.sql.*;
import java.util.*;
public class Student {
private String name;
private String sex;
private String zhuanye;
private int age;
private String number;
public static void main(String[] args) {
//
try{
Student student, newStudent;
List list = Student.loadRecord("");
System.out.println("original---------------------------");
for (int i=0; i<list.size();i++) {
student = (Student) list.get(i);
System.out.printf("%s\n",student);
}

//to test add method
newStudent = new Student();
newStudent.setName("New");
newStudent.add();
list = Student.loadRecord("");
System.out.println("a new Student added---------------------------");
for (int i=0; i<list.size();i++) {
student = (Student) list.get(i);
System.out.printf("%s\n",student);
}
}
catch(Exception ex) {
System.out.println("Exeption: " + ex.getMessage());
}


//

try{
String url="jdbc:odbc:student_information";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(java.lang.ClassNotFoundException e){
System.err.print("ClassNotFoundException");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"","");
Statement stmt= con.createStatement();

ResultSet rs=stmt.executeQuery("Select * from student_information");
while(rs.next())
{
for(int i=1;i<6;i++)
System.out.println("this number "+i+" information:"+rs.getString(i));
}
con.close();
}
catch(SQLException ex){
while(ex!=null){
System.out.println("Exception be catched!");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
ex = ex.getNextException();
}


}
}

//set get gouzaohanshu
public Student() {
setAttributes();
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getZhuanye() {
return zhuanye;
}
public void setZhuanye(String zhuanye) {
this.zhuanye= zhuanye;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNumber(){
return number;
}
public void setNumber(String number) {
this.number = number;
}


private void setAttributes() {
name = "";
sex="";
zhuanye="";
age=0;
number="";
}
//set get gouzaohanshu
//5yaoqiu
public static ArrayList loadRecord(String condition) throws Exception{
ArrayList<Student> list=new ArrayList<Student>();
Student student=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student_information", "", "");
String sql=" select name " + " from student_information " ;
if(condition!="")
sql = sql + " where " + condition;
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
//System.out.printf("Employee ID: %d\n",rs.getLong(1));
student = new Student();
student.setName(rs.getString(1));
student.setSex(rs.getString(2));
student.setZhuanye(rs.getString(3));
student.setAge(rs.getInt(4));
student.setNumber(rs.getString(7));
//
list.add(student);
}
rs.close();
pstmt.close();
con.close();
return list;
}

//5yaoqiu
//toString
public String toString() {
return String.format("%-10s%-10s%-30s%-3d%-10d",name,sex,zhuanye,age,number);
}
//toString
//1 yaoqiu

public void add() throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student_information", "", "");
PreparedStatement pstmt = con.prepareStatement( "insert into student_information (name,sex,zhuanye,age,number) values ('" + name +"','"+ sex+"','" + zhuanye + "','" + age + "','" +number + "')");
pstmt.execute();
pstmt = con.prepareStatement("select @@identity");
/*rs = stmt.executeQuery();
if (rs.next()) {
id = rs.getLong(1);
}*/
pstmt.close();

con.close();

}

//1 yaoqiu
}


我的执行结果
Exeption: [Microsoft][ODBC SQL Server Driver]无效的描述符索引
this number 1 information:戴建林
this number 2 information:male
this number 3 information:21
this number 4 information:computer
this number 5 information:041150109
this number 1 information:黄卿明
this number 2 information:male
this number 3 information:22
this number 4 information:computer
this number 5 information:041150102
this number 1 information:严元发
this number 2 information:male
this number 3 information:22
this number 4 information:computer
this number 5 information:041150103
this number 1 information:张太和
this number 2 information:male
this number 3 information:22
this number 4 information:computer
this number 5 information:041150104
this number 1 information:杨超钧
this number 2 information:male
......
不懂第一句异常是怎么回事啊。。 有谁知道的帮忙看看啊 先谢谢了。。
...全文
217 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
linyong07 2007-06-14
  • 打赏
  • 举报
回复
我改了student.setNumber(rs.getString(5));了,还是不行
不知道有谁知道的。高手帮忙一下啊。。看不出自己有什么错误
passstreet 2007-06-14
  • 打赏
  • 举报
回复
student.setNumber(rs.getString(7));????
fengmingjie 2007-06-14
  • 打赏
  • 举报
回复
我觉得这应该是个bug
是getXXX("")和sql语句的字段顺序不一致造成的,如
select a,b from ab
在访问的时候就不能先getXXX("b"),再getXXX("a"),而要先getXXX("a"),再getXXX("b")就没问题了
看看你取数据时的顺序是否和数据库中的字段的顺序是一致的

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧