java 怎样将查询到的数据库的内容显示在Swing界面上?
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 Class
NotFoundException,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显示
------解决方案--------------------
Java code
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();
}
}
------解决方案--------------------
先在外面定义一个StringBuild。
然后再循环里StringBuild.append(rs.getObject(1))
最后将字符串显示到JTextField里就行了