zhq0625
骡子骑马找骆驼
2017-06-09 02:37

便宜好的窗口 怎么java中插入一个动态的行或列可以添加修改数据,是不是需要用alter函数

  • java
  • 插入动态的行或列

根据姓名查询的视图
import java.awt.*;
import javax.swing.*;
public class ByName extends JPanel {
JTextField inputName;
JButton button;
ByNameHandleData handle;
ByName() {
inputName = new JTextField(12);
button = new JButton("确定");
add(new JLabel("输入姓名:"));
add(inputName);
add(button);
handle = new ByNameHandleData(); //负责按姓名去处理数据的处理者
handle.setJTextField(inputName);
button.addActionListener(handle);
}
}

负责按姓名去处理数据的处理者
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class ByNameHandleData implements ActionListener {

JTextField inputName;
PreQuery query; //第2天任务2 Query
ByNameHandleData() {
query=new PreQuery();
}
public void setJTextField(JTextField ... text) {
inputName = text[0];
}
public void actionPerformed(ActionEvent e) {
String na = inputName.getText(); //得到姓名
query.setDatabaseName("移动公司");
query.setSQL("SELECT * FROM 职员表 where 姓名='"+na+"'");
query.startQuery();
String ziduan[] =query.getColumnName();
String [][]record =query.getRecord();
DialogOne dialog = new DialogOne();
dialog.setZiduan(ziduan);
dialog.setRecord(record);
dialog.init();
dialog.setVisible(true);
}
}

根据出生日期查询的视图
import java.awt.*;
import javax.swing.*;
public class ByBirth extends JPanel {
JTextField inputYear,inputMonth,inputDay;
JButton button;
ByBirthHandleData handle;
ByBirth() {
inputYear = new JTextField(10);
inputMonth = new JTextField(6);
inputDay = new JTextField(6);
button = new JButton("确定");
add(new JLabel("输入年"));
add(inputYear);
add(new JLabel("输入月"));
add(inputMonth);
add(new JLabel("输入日"));
add(inputDay);
add(button);
handle = new ByBirthHandleData();//负责按出生去查询的数据处理者
handle.setJTextField(inputYear,inputMonth,inputDay);

button.addActionListener(handle);
}
}

负责按出生日期去查询的数据处理者
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class ByBirthHandleData implements ActionListener {

JTextField inputYear,inputMonth,inputDay;
PreQuery query;
ByBirthHandleData() {
query=new PreQuery();
}
public void setJTextField(JTextField ... text) {
inputYear = text[0];
inputMonth = text[1];
inputDay = text[2];
}
public void actionPerformed(ActionEvent e) {
String yy = inputYear.getText(); //得到年
String mm = inputMonth.getText(); //得到月
String dd = inputDay.getText(); //得到日
query.setDatabaseName("移动公司");
String SQL=
"SELECT * FROM 职员表 where year(出生)="+yy+
" and month(出生)="+mm+
" and day(出生)="+dd;
query.setSQL(SQL);
query.startQuery();
String ziduan[] =query.getColumnName();
String [][]record =query.getRecord();
DialogOne dialog = new DialogOne();
dialog.setZiduan(ziduan);
dialog.setRecord(record);
dialog.init();
dialog.setVisible(true);
}
}

将各个查询的视图集成到一个窗口中
import javax.swing.*;
import java.awt.*;
public class TWindow extends JFrame{
JTabbedPane p;
ByName byName;
ByBirth byBirth;
public TWindow(){
setBounds(100,100,800,200);
setVisible(true);
p=new JTabbedPane();
byName = new ByName();
byBirth = new ByBirth();
p.add("按姓名查询",byName);
p.add("按出生查询",byBirth);

p.validate();
add(p,BorderLayout.CENTER);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

    主类
    public class Day3_4 {

public static void main(String args[]) {
TWindow win = new TWindow();

}

}

主要编码如上

PreQuery 类
import java.sql.*;

public class PreQuery {
String databaseName=""; //数据库名
String SQL; //SQL语句
String [] columnName;

String [][] record;

public PreQuery() {
try{ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}
catch(Exception e) {
System.out.print(e);
}
}
public void setDatabaseName(String s) {

databaseName=s.trim();
}
public void setSQL(String SQL) {
this.SQL=SQL.trim();
}
public String[] getColumnName() {

   return columnName;

}
public String[][] getRecord() {

   return record;

}
public void startQuery() {
Connection con;
PreparedStatement sql;
ResultSet rs;
try {
String uri="jdbc:derby:"+databaseName+";create=true";
con=DriverManager.getConnection(uri);
sql=con.prepareStatement (SQL,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery();

ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
columnName=new String[columnCount];
for(int i=1;i<=columnCount;i++){
columnName[i-1]=metaData.getColumnName(i);
}
rs.last();
int recordAmount =rs.getRow();//结果集中的记录
record = new String[recordAmount][columnCount];
int i=0;
rs.beforeFirst();
while(rs.next()) {
for(int j=1;j<=columnCount;j++){
record[i][j-1]=rs.getString(j);
}
i++;
}
con.close();
}
catch(SQLException e) {
System.out.println("请输入正确的表名"+e);
}
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答