import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DB extends Exception{
public static Connection getConn() throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=GongGongXuanXiuKe";
String user = "USER3"; 
String password = "123456"; 
Connection conn = DriverManager.getConnection(url,user,password); 
return conn; 
}
public static Statement getStatement() throws ClassNotFoundException,SQLException{
Statement stmt=DB.getConn().createStatement();
            return stmt;
}
//   返回查询的结果集
public static ResultSet getResultSet(String sql) throws ClassNotFoundException,SQLException{
    ResultSet rs = DB.getStatement().executeQuery(sql);
    return rs;
}
//   根据返回的结果集映射到界面
public static void yingshe(String sql) throws ClassNotFoundException, SQLException{
ResultSet rs = DB.getResultSet(sql);
if(rs.next()){
**********************
***********************
*************************
************************
}
}
public static void close(Connection conn,Statement stmt,ResultSet rs) throws SQLException{ 
 if(conn!=null) 
 conn.close(); 
 if(rs!=null) 
 rs.close(); 
 if(stmt!=null)
 stmt.close(); 
 } 
}
不知道星号(*)那里要怎样写,才能把数据库中查询到内容在java编写的界面上的JTextField显示

解决方案 »

  1.   


    String a = "";
    try {
    if (rs.next()) {
    a = rs.getString(1);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }类似这种
      

  2.   

    回楼上,CSDN论坛富文本编辑器兼容性不好,在IE9下就没出现
      

  3.   

    先在外面定义一个StringBuild。然后再循环里StringBuild.append(rs.getObject(1))最后将字符串显示到JTextField里就行了