此代码将显示我表中的所有记录。 显示记录时,我想要编辑。 编辑任何记录时,应将其保存在数据库中。 请大家帮忙。
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class user_list2 extends JFrame
{
Container con;
int rows=0;
Object data1[][];
JScrollPane scroller;
JTable table;
user_list2()
{
con=getContentPane();
con.setLayout(new BorderLayout());
setSize(800,600);
setLayout(new BorderLayout());
Connection conn;
Statement st;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce)
{System.out.println("error");}
try
{
conn=DriverManager.getConnection("jdbc:odbc:abc");
st=conn.createStatement();
rs=st.executeQuery("select * from Patients Where Status='Discharged' ");
while(rs.next())
{
rows++;
}
data1=new Object[rows][10]; Object[] Colheads={"Patients Name","Age", "Sex","Admit Date","Description","Reference","No Of Dependent","Status","Type Of Room","ID"};
rs=st.executeQuery("Select * from Patients Where Status='Discharged' ");
for(int i1=0;i1<rows;i1++)
{
rs.next();
for(int j1=0;j1<10;j1++)
{
data1[i1][j1]=rs.getString(j1+1);
}
}
JTable table=new JTable(data1,Colheads);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(table,v,h);
con.add(jsp);
}
catch(SQLException ss)
{
System.out.println("error"+ss);
ss.printStackTrace();
}
}
public static void main(String[] ar)
{
user_list2 obj=new user_list2();
obj.setSize(900,600);
obj.show();
}
}
答案 0 :(得分:0)
这可能会对你有所帮助
使用TableModelListener监听表的状态更改并实现tableChanged
方法。
table.getModel().addTableModelListener(this);
public void tableChanged(TableModelEvent e) {
//write your db code to update
}