java图像开发学习——JTable之导入数据库
阅读原文时间:2023年07月08日阅读:3

package demo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class DeMO {

public DeMO(){  
    JFrame frame=new JFrame("暮雪超霸");  
    Container contentPane = frame.getContentPane();  
    JPanel panel=new JPanel();  
    JPanel panel1=new JPanel();  
    JPanel panel2=new JPanel();  
    JLabel jLabel=new JLabel("超霸");  
    JButton button=new JButton("开始");  
    JButton button2=new JButton("暂停");  
    JButton button3=new JButton("下一首");  
    contentPane.setLayout(new BorderLayout());  
    panel.add(jLabel);  
    panel.add(button);  
    panel.add(button2);  
    panel.add(button3);  
    contentPane.add(panel,BorderLayout.NORTH);

    JButton button4=new JButton("退出");  
    Conn con=new Conn();  
    ResultSet select = con.getSelect("select \*from chaoba");  
    Vector col = getCol(select);  
    Vector data = getData(select);  
    JTable jTable=new JTable(data,col);  
    JScrollPane jScrollPane=new JScrollPane();  
    jScrollPane.getViewport().add(jTable);  
    panel1.add(button4);  
    panel2.add(jScrollPane);  
    contentPane.add(panel1,BorderLayout.SOUTH);  
    contentPane.add(panel2,BorderLayout.CENTER);  
    frame.setSize(600, 400);  
    frame.setVisible(true);

    button.addMouseListener(new MouseAdapter() {  
        @Override  
        public void mouseClicked(MouseEvent arg0) {  
        System.out.println("开始");  
        }  
    });

    button2.addMouseListener(new MouseAdapter() {  
        @Override  
        public void mouseClicked(MouseEvent arg0) {  
        System.out.println("暂停");  
        }  
    });

}

public static void main(String[] args) {
new DeMO();
}

public Vector getCol(ResultSet rs){
Vector col=new Vector();
try {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
col.add(metaData.getColumnName(i));

    }

} catch (Exception e) {  
    // TODO 自动生成的 catch 块  
    e.printStackTrace();  
}

return col;

}
public Vector getData(ResultSet rs){
Vector data=new Vector();
try {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
Vector v=new Vector();
for(int i=1;i<=columnCount;i++)
{
v.addElement(rs.getString(i));
}
data.addElement(v);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

return data;

}
}

package demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Conn {

Connection con;  
public  Conn(){  
    try {  
        Class.forName("com.mysql.jdbc.Driver");  
        String url="jdbc:mysql://localhost:3306/chaoba?user=root&password=root&useUnicode=true&characterEncoding=UTF8";  
         con=DriverManager.getConnection(url);

    } catch (Exception e) {  
        // TODO 自动生成的 catch 块  
        e.printStackTrace();  
    }

}

public ResultSet getSelect(String sql){  
    Vector col=new Vector();  
    Vector data=new Vector();  
    ResultSet rs=null;  
        Statement statement;  
        try {  
            statement = con.createStatement();  
             rs = statement.executeQuery(sql);  
        } catch (SQLException e) {  
            // TODO 自动生成的 catch 块  
            e.printStackTrace();  
        }

    return rs;

}

}