java 学习 JTable列表输出sql表

MySql.CLASS
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class MySql {
    static Connection conn = null;
    Statement sta;
    ResultSet rs;
    String queryna,insertna,insertage,insertsex,insertph;
    public Statement getSta() {
        String pas, user, url;
        pas = "root";
        user = "root";
        url = "jdbc:mysql://localhost:3306/chj?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
        try {
            conn = DriverManager.getConnection(url, user, pas);
        } catch (Exception e) {
            System.out.println("error:" + e);
        }
        try {
            sta = conn.createStatement();
        } catch (Exception e) {
            System.out.println("error:"+e);
        }
        return sta;
    }
    public List<String> getQuery() {//把查询的sql数据放到list并返回这个list
        List<String> list = new ArrayList<>();
        try {
            sta = getSta();
                rs = sta.executeQuery("select * from anmo");
            while (rs.next()) {
                String age = rs.getString("age");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String phone = rs.getString("phonenum");
                list.add(age);
                list.add(name);
                list.add(sex);
                list.add(phone);
            }
            conn.close();
        } catch (Exception g) {
            System.out.println("error:" + g);
        }
        return list;
    }
    public void insertsql(){//插入方法,暂时没有调用
        sta=getSta();
        try {
            sta.executeUpdate("insert into anmo values ("+insertage+",'"+insertna+"','"+insertsex+"','"+insertph+"')");
            conn.close();
        }catch (Exception e){
            System.out.println("error:" + e);
        }
    }

}
MyJframe.CLASS
import javax.swing.*;
import java.util.List;

public class MyJFrame extends JFrame  {
    JPanel jPanel;
    JTable jTable;
        public MyJFrame(){
            this.setTitle("sql list");
            this.setBounds(600,200,600,400);
            List sqllist=new MySql().getQuery();
            int lisize=sqllist.size();//计算list里有多少个数据
            String[] col={"年龄","姓名","性别","电话号码"};
            String[][] tlist=new String[lisize/4][4];//计算有多少行数据,遍历到二维数组
            int li=0;
            for (int i = 0; i <lisize/4; i++) {
                for (int j = 0; j < 4; j++) {
                    tlist[i][j]= sqllist.get(li).toString();
                    li++;
                }
            }
            jPanel=new JPanel();
            jTable=new JTable(tlist,col);
            jPanel.add(new JScrollPane(jTable));
            this.add(jPanel);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setVisible(true);

        }
    }
public class demo {
    public static void main(String[] arg){
        new MyJFrame();
   }
}

运行截图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值