读取给的db文件,英雄联盟角色,有英雄绰号、名字、性变、价格和id。
需要在根目录下创建db文件夹,里面放入db后缀数据库。同时也要导入sqlite的一个jar包。
如何导入jar包
创建libs文件夹放入后,解压包:file-project-structure-dependencies 点击右上角+号,选jars即可。
如果有报错红线,鼠标放上去:more actions-surround with try/catach
核心:MyFrame
package S2.ui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseListener;
import java.sql.*;
import S2.utill.TableListener;
public class MyFrame<GetHeroInfo> extends JFrame {
private JScrollPane scrollPane = new JScrollPane();//带滚动条的面板
private JTable jTable = new MyTable();
private DefaultTableModel model = new DefaultTableModel(); //定义几行几列,行列名是什么
private TableListener listener = new TableListener(this);
public MyFrame() throws SQLException {
this.setSize(400,400);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(null);
this.setLocation(400,300);
//先初始化表格
initTable();
//初始数据
loadData();
//
jTable.addMouseListener(listener);
}
private void loadData() throws SQLException {
//查询数据库 获取结果集 再插入表格中
int currPage = 1;
int lim = 30;//每页10条
int off = (currPage - 1) * lim; //off 要跟随分页变化
Connection conn = DriverManager.getConnection("jdbc:sqlite:db/work10-12.db");
//6.取出数据
String sql2 = "select * from t_hero limit " + lim + " offset " + off;
Statement sta = conn.createStatement();
ResultSet rs = sta.executeQuery(sql2);
while (rs.next()) {
//如果有下一个就取出来
int hid = rs.getInt("h_id"); //列名
String hNike = rs.getString("h_nike");
String hName = rs.getString("h_name");
String hxing=rs.getString("h_sex");
String hPrice=rs.getString("h_price");
String[] data = {hid + "", hNike, hName,hxing,hPrice};
model.addRow(data);
}
}
private void initTable() {
String [] header = {"id","nike","name","sex","price"};
model.setColumnIdentifiers(header); //设置表头
jTable.setModel(model); //加入表格
scrollPane.setViewportView(jTable);
scrollPane.setBounds(20,20,350,250);
this.add(scrollPane);
}
private void getRowData(){
int row = jTable.getSelectedRow(); //获取点击的是第几行
model.getValueAt(row,0).toString();//获取第row 行的第一列表(下标是0)
}
public JScrollPane getScrollPane() {
return scrollPane;
}
public void setScrollPane(JScrollPane scrollPane) {
this.scrollPane = scrollPane;
}
public JTable getjTable() {
return jTable;
}
public void setjTable(JTable jTable) {
this.jTable = jTable;
}
public DefaultTableModel getModel() {
return model;
}
public void setModel(DefaultTableModel model) {
this.model = model;
}
}
MyTable:设置让表不可编辑(一般不用动)
package S2.ui;
import javax.swing.*;
public class MyTable extends JTable {
public MyTable(){
}
@Override
public boolean isCellEditable(int row, int column) {
return false; //不可编辑
}
}
TableListener用于监听鼠标双击
package S2.utill;
import S2.ui.MyFrame;
import S2.ui.MyFrame;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class TableListener implements MouseListener {
private MyFrame f;
public TableListener(MyFrame f) {
this.f = f;
}
@Override
public void mouseClicked(MouseEvent e) {
//双击表格
if (e.getClickCount() == 2){
//获取Model对象,才能获取里面的内容
int row = f.getjTable().getSelectedRow(); //获取点击了表格的第几行
String id = f.getModel().getValueAt(row,0).toString();
String nike = (String) f.getModel().getValueAt(row,1);
String name = (String) f.getModel().getValueAt(row,2);
JOptionPane.showMessageDialog(f,"id:"+id+"\nnike:"+nike+"\nname:"+name);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
最后TestMyFrame执行
package S2.test;
import S2.ui.MyFrame;
import java.sql.SQLException;
public class TestMyFrame {
public static void main(String[] args) throws SQLException {
new MyFrame().setVisible(true);
}
}