java swing下 关于数据库表单的操作,想单机添加后出现新的一行,能够添加信息,单击保存,能写入数据库
点击保存,能够将所添加的内容插到数据库表中,现在问题是点击添加后,怎么才能在表格中显示新的一行?求代码指点,以下是我的代码:importjava.awt.BorderLa...
点击保存,能够将所添加的内容插到数据库表中,现在问题是点击添加后,怎么才能在表格中显示新的一行? 求代码指点,以下是我的代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.ImageIcon;
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 asf
{
private JFrame f1=new JFrame();
private JTable t3;
private JPanel p1=new JPanel();
private JLabel l1;
private ImageIcon Hire_Setting_Pic;
private JButton Add_Button=new JButton("添加");
private JButton Delete_Button=new JButton("删除");
private JButton Edit_Button=new JButton("编辑");
private JButton Save_Button=new JButton("保存");
public asf()throws Exception{
super();
f1.setBounds(0,0,800,600);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table();
f1.add(p1);
p1.setBounds(330,0,470,60);
f1.setLayout(null);
Add_Button.setBounds(3,50,80,30);
f1.add(Add_Button);
Delete_Button.setBounds(3,100,80,30);
f1.add(Delete_Button);
Save_Button.setBounds(3,150,80,30);
f1.add(Save_Button);
Edit_Button.setBounds(3,200,80,30);
f1.add(Edit_Button);
Add_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t3.setVisible(false);
}
});
}
public void table() throws Exception
{
Vector columnVector=getColumnVector();
Vector dataVector=getDataVector();
JScrollPane scrollPane=new JScrollPane();
p1.add(scrollPane, BorderLayout.CENTER);//这里是重点
t3=new JTable(dataVector, columnVector);
t3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(t3);
t3.setSize(p1.getWidth(),p1.getHeight());
t3.setEnabled(false);
}
public static void main(String args[])throws Exception
{
asf a=new asf();
a.f1.setVisible(true);
}
public Vector getDataVector()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
//Connection conn = DriverManager.getConnection(url, user, password);
Connection conn=DriverManager.getConnection("jdbc:odbc:wzgl");
Statement st=conn.createStatement();
String sql="select * from Xianyang_Employment";
ResultSet rs=st.executeQuery(sql);
Vector dataVector =new Vector();
while (rs.next())
{
Vector rowVector=new Vector();
for(int i=1;i<=3;i++)
{
rowVector.add(rs.getString(i));
}
dataVector.add(rowVector);
}
return dataVector;
}
public Vector getColumnVector()
{
Vector columnVector=new Vector();
columnVector.add("描述");
columnVector.add("编制");
columnVector.add("补充");
return columnVector;
}
} 展开
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.ImageIcon;
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 asf
{
private JFrame f1=new JFrame();
private JTable t3;
private JPanel p1=new JPanel();
private JLabel l1;
private ImageIcon Hire_Setting_Pic;
private JButton Add_Button=new JButton("添加");
private JButton Delete_Button=new JButton("删除");
private JButton Edit_Button=new JButton("编辑");
private JButton Save_Button=new JButton("保存");
public asf()throws Exception{
super();
f1.setBounds(0,0,800,600);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table();
f1.add(p1);
p1.setBounds(330,0,470,60);
f1.setLayout(null);
Add_Button.setBounds(3,50,80,30);
f1.add(Add_Button);
Delete_Button.setBounds(3,100,80,30);
f1.add(Delete_Button);
Save_Button.setBounds(3,150,80,30);
f1.add(Save_Button);
Edit_Button.setBounds(3,200,80,30);
f1.add(Edit_Button);
Add_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t3.setVisible(false);
}
});
}
public void table() throws Exception
{
Vector columnVector=getColumnVector();
Vector dataVector=getDataVector();
JScrollPane scrollPane=new JScrollPane();
p1.add(scrollPane, BorderLayout.CENTER);//这里是重点
t3=new JTable(dataVector, columnVector);
t3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(t3);
t3.setSize(p1.getWidth(),p1.getHeight());
t3.setEnabled(false);
}
public static void main(String args[])throws Exception
{
asf a=new asf();
a.f1.setVisible(true);
}
public Vector getDataVector()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
//Connection conn = DriverManager.getConnection(url, user, password);
Connection conn=DriverManager.getConnection("jdbc:odbc:wzgl");
Statement st=conn.createStatement();
String sql="select * from Xianyang_Employment";
ResultSet rs=st.executeQuery(sql);
Vector dataVector =new Vector();
while (rs.next())
{
Vector rowVector=new Vector();
for(int i=1;i<=3;i++)
{
rowVector.add(rs.getString(i));
}
dataVector.add(rowVector);
}
return dataVector;
}
public Vector getColumnVector()
{
Vector columnVector=new Vector();
columnVector.add("描述");
columnVector.add("编制");
columnVector.add("补充");
return columnVector;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询