有水印,最后一个是JTableApp,jar包需要导入一下,步骤
File->ProjectStructure->Modules->选+号,然后找到本地的jar包加载->勾选后Apply就可以加载了
然后Student类:
package JTable.bean;
public class Student {
private String SNO;
private String SNAME;
private String SGENDER;
private String SBIRTHDATE;
public Student() {
super();
}
@Override
public String toString() {
return "Student{" +
"SNO='" + SNO + '\'' +
", SNAME='" + SNAME + '\'' +
", SGENDER='" + SGENDER + '\'' +
", SBIRTHDATE='" + SBIRTHDATE + '\'' +
'}';
}
public String getSNO() {
return SNO;
}
public void setSNO(String SNO) {
this.SNO = SNO;
}
public String getSNAME() {
return SNAME;
}
public void setSNAME(String SNAME) {
this.SNAME = SNAME;
}
public String getSGENDER() {
return SGENDER;
}
public void setSGENDER(String SGENDER) {
this.SGENDER = SGENDER;
}
public String getSBIRTHDATE() {
return SBIRTHDATE;
}
public void setSBIRTHDATE(String SBIRTHDATE) {
this.SBIRTHDATE = SBIRTHDATE;
}
public Student(String SNO, String SNAME, String SGENDER, String SBIRTHDATE) {
this.SNO = SNO;
this.SNAME = SNAME;
this.SGENDER = SGENDER;
this.SBIRTHDATE = SBIRTHDATE;
}
}
StudentDao类:
package JTable.dao;
import JTable.bean.Student;
import java.sql.SQLException;
import java.util.List;
public interface StudentDao {
public List<Student> findAll() throws SQLException;
public int add(Student student);
public int delete(Student student);
public int update(Student student);
}
StudentDaoImpl类:
package JTable.dao;
import JTable.DB;
import JTable.bean.Student;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.QueryRunner;
import java.sql.SQLException;
import java.util.List;
public class StduentDaoImpl implements StudentDao {
private QueryRunner queryRunner = new QueryRunner();
@Override
public List<Student> findAll() throws SQLException {
String sql = "select * from student";
List<Student> list = null;
list = queryRunner.query(DB.getConnection(),sql,new BeanListHandler<Student>(Student.class));
return list;
}
@Override
public int add(Student student) {
String sql = "INSERT INTO STUDENT(SNO,SNAME,SGENDER,SBIRTHDATE) VALUES (?,?,?,?)";
Object[] obj = {student.getSNO(),student.getSNAME(),student.getSGENDER(),student.getSBIRTHDATE()};
int result = 0 ;
try{
result = queryRunner.update(DB.getConnection(),sql,obj);
}catch (SQLException e)
{
e.printStackTrace();
}
return result;
}
@Override
public int delete(Student student) {
String sql= "DELETE FROM STUDENT WHERE SNO=?";
Object[] obj = {student.getSNO()};
int result = 0 ;
try{
result = queryRunner.update(DB.getConnection(),sql,obj);
}catch (SQLException e)
{
e.printStackTrace();
}
return result;
}
@Override
public int update(Student student) {
String sql= "update student set SNAME=? ,SGENDER=?,SBIRTHDATE =? WHERE SNO=?";
Object[] obj={student.getSNAME(),student.getSGENDER(),student.getSBIRTHDATE(),student.getSNO()};
int result = 0 ;
try{
result = queryRunner.update(DB.getConnection(),sql,obj);
}catch (SQLException e)
{
e.printStackTrace();
}
return result;
}
}
package JTable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB {
private static String url = "jdbc:mysql://localhost:3306/java_student";
private static String user = "root";
private static String password = "root";
private static Connection connection;
public static Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,user,password);
}catch (SQLException | ClassNotFoundException e){
e.printStackTrace();
}
return connection;
}
}
JTableApp类:
package JTable;
import JTable.bean.Student;
import JTable.dao.StudentDao;
import JTable.dao.StduentDaoImpl;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;
public class JTableApp extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private JTextField aText,bText,cText,dText;
private int i, j;
public JTableApp() throws SQLException {
super();
setTitle("Table App");
setBounds(300,200,1000,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] title={"学号","姓名","性别","出生年月日"};
//实例化TableModel
StudentDao studentDao= new StduentDaoImpl();
List<Student> data =studentDao.findAll();
//注意这里data.size()不要+1!!!!!!!!!!!
String[][] data_string =new String[data.size()][10];
for (i=0;i<data.size();i++){
String[] strings= data.get(i).toString().split("'");
for (j=0;j<strings.length;j++){
data_string[i][j]="0";
}
}
tableModel = new DefaultTableModel(data_string,title);
//实例化JTable对象
table = new JTable(tableModel);
//初始化界面
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportView(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
//设置表格选择模式:单选
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
aText = new JTextField(8);
bText = new JTextField(8);
cText = new JTextField(5);
dText = new JTextField(8);
JPanel panel = new JPanel();
panel.add(new JLabel("学号:"));
panel.add(aText);
panel.add(new JLabel("姓名:"));
panel.add(bText);
panel.add(new JLabel("性别:"));
panel.add(cText);
panel.add(new JLabel("出生年月日:"));
panel.add(dText);
JButton btnAdd = new JButton("添加");
JButton btnDelete = new JButton("删除");
JButton btnEdit = new JButton("修改");
JButton btnFind= new JButton("查询");
panel.add(btnAdd);
panel.add(btnDelete);
panel.add(btnEdit);
panel.add(btnFind);
getContentPane().add(panel,BorderLayout.SOUTH);
//选择表格的行并为文本框赋值
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//获取选择的行号
int row = table.getSelectedRow();
Object a = tableModel.getValueAt(row,0);
Object b = tableModel.getValueAt(row,1);
Object c = tableModel.getValueAt(row,2);
Object d = tableModel.getValueAt(row,3);
//把单元格的值赋给文本框
aText.setText(a.toString());
bText.setText(b.toString());
cText.setText(c.toString());
dText.setText(d.toString());
}
});//结束
//添加功能
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String[] values = {aText.getText(),bText.getText(),cText.getText(),dText.getText()};
//表格添加一行
tableModel.addRow(values);
// int rowCount = table.getRowCount();//获取表中总行数
//操作数据库的代码
Student student= new Student(aText.getText(),bText.getText(),cText.getText(),dText.getText());
StudentDao studentDao= new StduentDaoImpl();
studentDao.add(student);
}
});
//删除
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = 0;
row = table.getSelectedRow();
if(row!=-1)//已选择行
{
int tip = JOptionPane.showConfirmDialog(null,"确定要删除该行?",
"提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(tip == JOptionPane.OK_OPTION){
tableModel.removeRow(row);
//操作数据库的代码
Student student= new Student(aText.getText(),bText.getText(),cText.getText(),dText.getText());
StudentDao studentDao= new StduentDaoImpl();
studentDao.delete(student);
}else
{
return;
}
}
else{
JOptionPane.showMessageDialog(null,"请选择要删除的行!",
"提示",JOptionPane.WARNING_MESSAGE);
}
}
});
//编辑
btnEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = 0;
row = table.getSelectedRow();
if(row!=-1){
tableModel.setValueAt(aText.getText(),row,0); //表格中第Row行第0列 这个单元格赋值。
tableModel.setValueAt(bText.getText(),row,1);
tableModel.setValueAt(cText.getText(),row,2);
tableModel.setValueAt(dText.getText(),row,3);
//操作数据库的代码
Student student= new Student(aText.getText(),bText.getText(),cText.getText(),dText.getText());
StudentDao studentDao= new StduentDaoImpl();
studentDao.update(student);
} else{
JOptionPane.showMessageDialog(null,"请选择要修改的行!",
"提示",JOptionPane.WARNING_MESSAGE);
}
}
});
//查询
btnFind.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StudentDao studentDao= new StduentDaoImpl();
try {
List<Student> data =studentDao.findAll();
for(i=0;i<data.size();i++){
String[] strings= data.get(i).toString().split("'");
for (j=0;j<strings.length;j++){
if((j+1)%2==0){
if(j-1==0){
tableModel.setValueAt(strings[j],i,0);
}else{
tableModel.setValueAt(strings[j],i,(j-1)/2);
}
}
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) throws Exception{
JTableApp app = new JTableApp();
app.setVisible(true);
}
}
主要卡在写查询类的时候:
1:List转换成string的时候,用Split分割
2:tableModel.setValueAt(strings[j],i,0);这个必须是已经有的行和列才可以set值
3:delete的时候,from的from不是form,一度还怀疑idea坏了吧!!!以后谨记