import java.util.*;
import java.sql.*;/**
 *
 * <p>Title: newsite</p>
 * <p>Description: 管理新闻的类</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class Op_News {
  private Connection conn;  public Op_News() {
  }  public String to_String(String s) {
    try {
      return new String(s.getBytes("ISO8859-1"),"GBK");
    } catch(Exception e) {
      return s;
    }
  }
  public Connection makeConnection() {
    Connection conn = null;
    String sql = "jdbc:mysql://localhost/newsite?user=root&password=zenggui&useUnicode=true&characterEncoding=8859_1";
    try{
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      conn = DriverManager.getConnection(sql);
    } catch(SQLException e) {
      System.out.println("SQL error1");
    } catch(Exception e) {
      System.out.println("Class NOT FOUND1");
    }
    return conn;
  }
  public Collection news_Search() {
    String strsql = "select * from news";
    Collection newlist = new ArrayList();
    try {
      conn = makeConnection();
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(strsql);
      while(rs.next()) {
        News news = new News();
        news.setTitle(rs.getString("title"));
        news.setType(rs.getString("type"));
        news.setSource(rs.getString("source"));
        news.setContent(rs.getString("Content"));
        newlist.add(news);
      }
    } catch(SQLException e) {
     System.out.println("SQL error2");
     e.printStackTrace();
    } catch(Exception e) {
      System.out.println("CLASS NOT FOUND2");
      e.printStackTrace();
    }
    return newlist;
  }
  public static void main(String[] args) {
    Op_News carton = new Op_News();
    Collection coll = carton.news_Search();
    Iterator cail = coll.iterator();
    while(cail.hasNext()) {
      News hasnew = (News)cail.next();
      System.out.println(hasnew.getTitle());
      System.out.println(hasnew.getType());
      System.out.println(hasnew.getSource());
      System.out.println(hasnew.getContent());  
    }   
  }
}结果:
SQL error2
java.sql.SQLException: Column 'type' not found.
         at com.mysql.jdbc.ResultSet.findColumn<ResultSet.java:910>
         at com.mysql.jdbc.ResultSet.getString<ResultSet.java:4942>
         at Op_News.news_Search<Op_News.java:50>
         at Op_News.main<Op_News.java:66>结果表明error2,所以连接没有问题,是ResultSet的问题,这是什么问题啊??

解决方案 »

  1.   

    这个错误信息说的很明白了呀,找不到Type这个列。检查一下了。数据表里是不是有这个东西。
      

  2.   

    有啊,都有这些啊,数据表里用的是varchar类型的
      

  3.   

    改这个试试:String strsql = "select title,type,source,content from news";
      

  4.   

    这个我也试了,最早的时候是这样的  String strsql = "select title from news";
    这样也不行啊
      

  5.   

    楼主的字断是不是mysql的保留字啊
    试试'type'
      

  6.   

    如果有这个字段的话,很有可能type是sql的保留字。