JAVA连接数据库后 ,如何窗口或者弹出对话框输出查询结果。在线等!原查询代码如下。
代码如下。importjava.sql.*;publicclassselection_all{publicstaticvoidmain(String[]srg){Stri...
代码如下。
import java.sql.*;
public class selection_all {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 连接服务器和数据库test
String userName = "sa";
String userPwd = "123";
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement s = dbConn.createStatement();
ResultSet rs=s.executeQuery("Select * from student");
while(rs.next()){
System.out.println(rs.getString("name")+"\t"+rs.getString("no")+"\t");
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在是输出在窗口之外,我想在新建窗口输出或者对话框输出要如何实现,在线等,谢谢了 展开
import java.sql.*;
public class selection_all {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 连接服务器和数据库test
String userName = "sa";
String userPwd = "123";
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement s = dbConn.createStatement();
ResultSet rs=s.executeQuery("Select * from student");
while(rs.next()){
System.out.println(rs.getString("name")+"\t"+rs.getString("no")+"\t");
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在是输出在窗口之外,我想在新建窗口输出或者对话框输出要如何实现,在线等,谢谢了 展开
1个回答
展开全部
package test;
import java.sql.*;
import javax.swing.*;
public class selection_all {
public static void main(String[] srg) {
// String driverName = "org.gjt.mm.mysql.Driver"; // 加载JDBC驱动
// String dbURL = "jdbc:mysql://localhost:3306/project"; // 连接服务器和数据库test
// String userName = "root";
// String userPwd = "root";
String driverName = "jdbc:mysql://localhost:3306/project"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 连接服务器和数据库test
String userName = "sa";
String userPwd = "123";
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement s = dbConn.createStatement();
ResultSet rs = s.executeQuery("select * from person");
String str = "<html><table>";
while (rs.next()) {
System.out.println(rs.getString("name") + "\t" + rs.getString("no") + "\t");
// str += rs.getString("name") + "\t" + rs.getString("no") + "\t\n";
str +="<tr><td>" + rs.getString("name") + "</td><td>" + rs.getString("no") + "</td></tr>";
// System.out.println(rs.getString("name") + "\t" + rs.getString("password") + "\t");
// str +="<tr><td>" + rs.getString("name") + "</td><td>" + rs.getString("password") + "</td></tr>";
}
str += "</table></html>";
s.close();
JDialog dialog = new JDialog();
dialog.setBounds(200, 200, 500, 300);
JLabel jl = new JLabel();
jl.setVerticalAlignment(SwingConstants.TOP);
dialog.add(jl);
jl.setText(str);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.*;
import javax.swing.*;
public class selection_all {
public static void main(String[] srg) {
// String driverName = "org.gjt.mm.mysql.Driver"; // 加载JDBC驱动
// String dbURL = "jdbc:mysql://localhost:3306/project"; // 连接服务器和数据库test
// String userName = "root";
// String userPwd = "root";
String driverName = "jdbc:mysql://localhost:3306/project"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 连接服务器和数据库test
String userName = "sa";
String userPwd = "123";
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement s = dbConn.createStatement();
ResultSet rs = s.executeQuery("select * from person");
String str = "<html><table>";
while (rs.next()) {
System.out.println(rs.getString("name") + "\t" + rs.getString("no") + "\t");
// str += rs.getString("name") + "\t" + rs.getString("no") + "\t\n";
str +="<tr><td>" + rs.getString("name") + "</td><td>" + rs.getString("no") + "</td></tr>";
// System.out.println(rs.getString("name") + "\t" + rs.getString("password") + "\t");
// str +="<tr><td>" + rs.getString("name") + "</td><td>" + rs.getString("password") + "</td></tr>";
}
str += "</table></html>";
s.close();
JDialog dialog = new JDialog();
dialog.setBounds(200, 200, 500, 300);
JLabel jl = new JLabel();
jl.setVerticalAlignment(SwingConstants.TOP);
dialog.add(jl);
jl.setText(str);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
追问
直接运行是可以的,但是我是在另外一个类的监听器语句中引用就无法弹出窗口。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |