在Tree上做监听,点击某一项后,把查询数据,插进相应的model,jTable.setModel(model)。
步骤1、做JTree的事件监听器,有 *** 作就
步骤2、新建类 MyTableModel extends DefaultTableModel {
public void addData() { }
} 这个类就是用来保存数据的
步骤3、jTable.setModel(new MyTableModel())
jTable.setVisible(true)
那只能创建一层的JTree ?
import java.sql.Connectionimport 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 JTreeDemo2 extends JFrame {
private Connection con
private Statement ste = null
ResultSet rs
JTreeDemo2() {
super("JTreeDemo")
this.setSize(200, 200)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
OpenDb()
DefaultMutableTreeNode node = new DefaultMutableTreeNode("所有表")
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 = "show tables"
// 光标的相对位置
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.mysql.jdbc.Driver"
String connection = "jdbc:mysql://localhost:3306/test"
// 数据库用户名
String user = "root"
// 数据库密码
String password = "root"
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 JTreeDemo2()
}
}
一句两句说不清楚,给个代码大家交流哈,我也是学编程的import java.sql.*
import java.io.*
public class Test13{
public static void main(String[] args) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection conn = DriverManager.getConnection("jdbc:odbc:folkmusic")
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from main")
Class.forName("oracle.jdbc.driver.OracleDriver")
Connection conn1 = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:java","java1","test123")
//conn1.setAutoCommit(false)
while(rs.next()){
String content = rs.getString("content")
BufferedWriter bw = new BufferedWriter(new FileWriter("tmpfile.txt"))
bw.write(content)
bw.flush()
bw.close()
PreparedStatement stmt1 = conn1.prepareStatement("insert into main (content) values (?)")
InputStream is = new FileInputStream(new File("tmpfile.txt")
stmt1.setBinaryStream(1,is,is.available())
stmt1.executeUpdate()
}
//conn1.commit()
rs.close()
//stmt1.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)