已经可以用连接数据库,并能读出数据库中表,但是要想在读出表中的字段,该怎么办啊?求指教,这是我的代码
package com.yang.tree;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class eee extends JFrame {
private Connection con;
private Statement ste = null;
ResultSet rs;
eee() {
super("eee");
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OpenDb();
DefaultMutableTreeNode node = new DefaultMutableTreeNode("db_yang");
createNodes(node, "0");
JTree tree = new JTree(node);
this.add(tree);
this.setVisible(true);
}
public void createNodes(DefaultMutableTreeNode parent, String ParentID){
DefaultMutableTreeNode temp = null;
String strsql = "select name from sysobjects where xtype='U'";
//获取表中所有字段:Select Name FROM SysColumns Where id=Object_Id('TableName')
// 光标的相对位置
try{
rs = ste.executeQuery(strsql);
while (rs.next()){
temp = new DefaultMutableTreeNode(rs.getString(1));
parent.add(temp);
}
} catch(Exception e){
System.out.println(e);
}
}
// 连接数据库
void OpenDb(){
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connection = "jdbc:sqlserver://localhost:1433; "
+ "DatabaseName=db_yang";
// 数据库用户名
String user = "sa";
// 数据库密码
String password = "19921221";
try {
Class.forName(driver);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
con = DriverManager.getConnection(connection, user, password);
ste = con.createStatement();
}catch (SQLException e){
e.printStackTrace();
}
}
public static void main(String[] argv) throws Exception {
new eee();
}
}
这是我读出来数据库中的表,比如想把表table_1里的字段读出来,并且插入到table_1节点下,该怎么办啊?
------解决思路----------------------
SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='表名'