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
......
不懂第一句异常是怎么回事啊。。 有谁知道的帮忙看看啊 先谢谢了。。
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
......
不懂第一句异常是怎么回事啊。。 有谁知道的帮忙看看啊 先谢谢了。。
不知道有谁知道的。高手帮忙一下啊。。看不出自己有什么错误
是getXXX("")和sql语句的字段顺序不一致造成的,如
select a,b from ab
在访问的时候就不能先getXXX("b"),再getXXX("a"),而要先getXXX("a"),再getXXX("b")就没问题了
看看你取数据时的顺序是否和数据库中的字段的顺序是一致的