把sql查到的表显示到文本区,新手啊不会,求指导
帮忙改下
public class txt extends JFrame {
private static final String FILE_NAME ="C:\\ab.txt";//文件名可改
JButton b1;//按钮
JTextArea txt1;//文本区
public txt(){
this.setTitle("txt文件载入");
b1=new JButton("点击载入文件");
b1.addActionListener(new ActionListener(){
//发生操作事件时,调用该对象的 actionPerformed 方法。
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
loadFile(FILE_NAME);
}
});
this.add(b1,BorderLayout.SOUTH);//按钮位置为下中间
txt1=new JTextArea();
this.add(txt1,BorderLayout.CENTER);//文本居中
}
private void loadFile(String filename){
Scanner sc=null;//文本扫描器
try{
sc=new Scanner(new File(filename));
txt1.setText("");
// 如果在此扫描器的输入中存在另一行
while(sc.hasNextLine()){
txt1.append(sc.nextLine());
txt1.append("\n");
}
}
//没有找到文件异常监测
catch(FileNotFoundException e){
e.printStackTrace();
}
//异常监测
finally{
if(sc!=null)
sc.close();
}
}
public static void main(String[] args){
txt wnd=new txt();
wnd.setSize(600,500);//窗口大小
wnd.setVisible(true);//设置窗口可见
}
}
下面试我连接数据库成功的代码
package pkg;
import java.sql.*;
public class Main {
public static void main(String[]args)
{
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=test";
String userName="sa";
String userPwd="123456";
try{
Class.forName(driverName);
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接成功");
}
catch(Exception e){
e.printStackTrace();
System.out.println("连接失败");
}
}
}