数据库连接的基本问题,100分等待,不够可加..

wlf427 2004-05-08 08:06:12
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.sql.*;

class PersonnelRecordTable extends JFrame {

.............................

private void dataQuery(){
try{
ResultSetMetaData rsmd = conIns.rs.getMetaData();
conIns.stm = conIns.con.createStatement();

if(txtFieldQue.getText().trim().equals(""))
conIns.rs = conIns.stm.executeQuery("select * from personnel");
else
conIns.rs = conIns.stm.executeQuery("select * from personnel where project_ID = "
+ txtFieldQue.getText().trim());
//初始化向量对象
vect.removeAllElements();
//更新表格内容
tm.fireTableStructureChanged();

while(conIns.rs.next()){
Vector rec_vector = new Vector();
for(int i =1; i<=rsmd.getColumnCount(); i++){
//从结果集中取数据放入向量rec_vector中
rec_vector.addElement(conIns.rs.getObject(i));
}

//向量rec_vector加入向量vect中
vect.addElement(rec_vector);
}

//更新表格,显示向量vect的内容
tm.fireTableStructureChanged();
//查询完成后,关闭该SQL
conIns.stm.close();
//重新执行SQL
openDB();

} catch(Exception e){
System.out.print("Error in Query Data." + e);
System.exit(1);
}
}


//打开表
private void openDB() {
try{
conIns.stm = conIns.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
conIns.rs = conIns.stm.executeQuery("select * from personnel");
conIns.rs.next();
} catch(Exception e){
System.out.print("Error in open table." + e);
System.exit(1);
}
}



//提取字段信息
private void loadData(){
try{
ResultSetMetaData rsmd = conIns.rs.getMetaData();
for(int i =1; i<=fields.size(); i++)
{
String value = " ";
String name = rsmd.getColumnName(i).trim();
value = conIns.rs.getString(name);
JTextField txtField = (JTextField) fields.get(i-1);
txtField.setText(value);
}
}catch(Exception e){
System.out.print("Error in load data." + e);
System.exit(1);
}
}

private void enableButton() {
try{
button_first.setEnabled(!conIns.rs.isFirst());
button_previous.setEnabled(!conIns.rs.isFirst());
button_next.setEnabled(!conIns.rs.isLast());
button_last.setEnabled(!conIns.rs.isLast());
}catch (Exception e){
System.out.print("Error in enable button" +e);
System.exit(1);

}
}

//-----------------------------------------------------------
//面板panelLabel
private JPanel panelLabel = new JPanel();

//标签名
private String labels[] ={ "人员编号" , "姓名",
"特长", "参与的项目编号"
};


//面板panelText
private JPanel panelText = new JPanel();
//文本框


//面板panel_label_text
private JPanel panel_label_text = new JPanel();


//面板panelButtonGroup1
private JPanel panelButtonGroup1 = new JPanel();
//按钮组1
private JButton button_first = new JButton("第一条");
private JButton button_previous= new JButton("前一条");
private JButton button_next= new JButton("下一条");
private JButton button_last= new JButton("最后一条");


//面板panelButtonGroup2
private JPanel panelButtonGroup2 = new JPanel();
//按钮组2
private JButton button_insert= new JButton("新增");
private JButton button_update= new JButton("修改");
private JButton button_delete= new JButton("删除");
private JButton button_commite= new JButton("提交");
private JButton button_cancel= new JButton("取消");
private JButton button_quit= new JButton("退出");


//面板panelQuery
private JPanel panelQuery = new JPanel();
//label
private JLabel label_que = new JLabel("项目编号", 0);
//文本框
private JTextField txtFieldQue = new JTextField(40);
//按钮
private JButton button_que = new JButton("查询");


//面板panelTable
private JPanel panelTable = new JPanel();
private JScrollPane panelScroll;
//表格
//声明向量对象
//private Vector rows = new Vector();
//private Vector columnname = new Vector();
private Vector vect = new Vector();

//设定数据
//声明一个类AbstractTableModel对象
//实现AbstractTableModel对象tm中的方法
private AbstractTableModel tm = new AbstractTableModel(){
//取得表格列数
public int getColumnCount(){
return labels.length;
}
//取得表格行数
public int getRowCount(){
return vect.size();
}
//取得单元格中的属性值
public Object getValueAt(int aRow, int aColumn) {
Vector row = (Vector)vect.elementAt(aRow);
return row.elementAt(aColumn);
}
//设置表格列名
public String getColumnName(int column){
if (labels[column] != null) {
return labels[column];
} else {
return "";
}
}
//数据模型不可编辑,该方法设置为空
public void setValueAt(Object value, int row,int column){}
//取得列所属对象类
public Class getColumnClass(int column) {
int type;
try {
type = conIns.rs.getMetaData().getColumnType(column+1);
}
catch (SQLException e) {
return super.getColumnClass(column);
}

switch(type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;

case Types.BIT:
return Boolean.class;

case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;

case Types.BIGINT:
return Long.class;

case Types.FLOAT:
case Types.DOUBLE:
return Double.class;

case Types.DATE:
return java.sql.Date.class;

default:
return Object.class;
}
}
//设置单元格不可编辑,为缺省实现
public boolean isCellEditable(int row,int column){
return false;
}
};

private JTable table_que;


//面板panel_que_table
private JPanel panel_que_table = new JPanel();

//面板panel_buttongroup1_text_buttongruop2
private JPanel panel_buttongroup1_label_text_buttongruop2 = new JPanel();


//整体
private Container contentPane = new Container();

private Vector fields = new Vector();
private conDB conIns;
private boolean isInsert = false;
}

上面是我的程序的这个模块...我每次在添加数据时就报错..
说是:Error in commit data.java.sql.SQLException:[Microsoft]
[ODBC Microsoft Access Driver]行中有错误
我的运行环境是:jdk1.4
...全文
41 19 点赞 打赏 收藏 举报
写回复
19 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
孙亖 2004-05-13
http://expert.csdn.net/Expert/TopicView1.asp?id=3063668
  • 打赏
  • 举报
回复
wlf427 2004-05-13
............................................
//面板panel_buttongroup1_label_text_buttongruop2设置
panel_buttongroup1_label_text_buttongruop2.setLayout(new BorderLayout());
panel_buttongroup1_label_text_buttongruop2.add(panelButtonGroup1,"North");
panel_buttongroup1_label_text_buttongruop2.add(panel_label_text,"Center");
panel_buttongroup1_label_text_buttongruop2.add(panelButtonGroup2,"South");

Border operEtched=BorderFactory.createEtchedBorder();
Border operTitled=BorderFactory.createTitledBorder
(operEtched,"数据库操作显示",TitledBorder.CENTER,
TitledBorder.TOP,new Font("SansSerif",Font.BOLD,20),
Color.red);
panel_buttongroup1_label_text_buttongruop2.setBorder(operTitled);


//面板panelQuery设置
panelQuery.setLayout(new GridLayout(1,3));
button_que.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dataQuery();
}
});
panelQuery.add(label_que);
panelQuery.add(txtFieldQue);
panelQuery.add(button_que);

//表格设置
//建立表格
table_que = new JTable(tm);
table_que.setToolTipText("显示全部查询结果");
table_que.setCellSelectionEnabled(false);
table_que.setShowVerticalLines(true);
table_que.setShowHorizontalLines(true);
//table_que.setEnabled(false);

//建立scroll Pane
panelScroll = new JScrollPane(table_que);

//面板panelTable设置
panelTable.setLayout(new BorderLayout());
panelTable.add(panelScroll,"Center");

Border queryEtched=BorderFactory.createEtchedBorder();
Border queryTitled=BorderFactory.createTitledBorder
(queryEtched,"数据库查询显示",TitledBorder.CENTER,
TitledBorder.TOP,new Font("SansSerif",Font.BOLD,20),
Color.red);
panelQuery.setBorder(queryTitled);
...........................................
private void insertData() {
isInsert = true;
panelButtonGroup1.setVisible(false);
button_insert.setEnabled(false);
button_update.setEnabled(false);
button_delete.setEnabled(false);
button_commite.setEnabled(true);
button_cancel.setEnabled(true);
for(int i=1; i<=fields.size(); i++){
JTextField txtField = (JTextField) fields.get(i-1);
txtField.setText("");
txtField.setEditable(true);
txtField.setBackground(Color.white);

}
try{
conIns.rs.moveToInsertRow();
}catch(Exception e){
System.out.print("Error in insert Data." +e);
System.exit(1);

}
}


private void updateData(){
panelButtonGroup1.setVisible(false);
button_insert.setEnabled(false);
button_update.setEnabled(false);
button_delete.setEnabled(false);
button_commite.setEnabled(true);
button_cancel.setEnabled(true);
for(int i=1; i<=fields.size(); i++){
JTextField txtField = (JTextField) fields.get(i-1);
txtField.setEditable(true);
txtField.setBackground(Color.white);
}
try{
conIns.rs.moveToCurrentRow();
}catch(Exception e){
System.out.print("Error in update Data." +e);
System.exit(1);

}
}


private void commitData(){
try{
ResultSetMetaData rsmd = conIns.rs.getMetaData();
//conIns.rs.moveToCurrentRow();

for(int i =1 ; i<= fields.size(); i++) {
JTextField txtField = (JTextField)fields.get(i-1);
String value = txtField.getText().trim();
String name = rsmd.getColumnName(i).trim().toUpperCase();
conIns.rs.updateString(name, value);
txtField.setEditable(false);
}

if(isInsert)
conIns.rs.insertRow();
else
conIns.rs.updateRow();

conIns.rs.moveToCurrentRow();
panelButtonGroup1.setVisible(true);
button_insert.setEnabled(true);
button_update.setEnabled(true);
button_delete.setEnabled(true);
button_commite.setEnabled(false);
button_cancel.setEnabled(false);
conIns.rs.refreshRow();
loadData();
enableButton();
}catch(Exception e){
System.out.print("Error in commit data." +e);
System.exit(1);
}

}


private void cancelData(){
try{
for(int i =1; i<=fields.size(); i++){
JTextField txtField = (JTextField)fields.get(i-1);
txtField.setEditable(false);
}
conIns.rs.moveToCurrentRow();
panelButtonGroup1.setVisible(true);
button_insert.setEnabled(true);
button_update.setEnabled(true);
button_delete.setEnabled(true);
button_commite.setEnabled(false);
button_cancel.setEnabled(false);
conIns.rs.refreshRow();
loadData();
enableButton();
}catch(Exception e){
System.exit(1);

}
}


private void deleteData(){
try{
int CursorPos = conIns.rs.getRow();
conIns.rs.deleteRow();
conIns.rs.absolute(CursorPos-1);
conIns.rs.refreshRow();
loadData();
enableButton();
}catch(Exception e){
System.out.print("Error in delete Data." +e);
System.exit(1);

}
}

我想问题是出在这两个地方吧希望可以给点提示
  • 打赏
  • 举报
回复
lightsword 2004-05-11
顶一下.
  • 打赏
  • 举报
回复
yidengjiang 2004-05-10
aceess还可以
我就是用这个
  • 打赏
  • 举报
回复
BenBenBoy 2004-05-10
怎么没看到你的数据添加部分代码?
  • 打赏
  • 举报
回复
inside777 2004-05-10
数据库的连接在哪里呢。这段程序不全
  • 打赏
  • 举报
回复
jinsfree 2004-05-10
好长,帮你顶
  • 打赏
  • 举报
回复
superryu 2004-05-10
好长啊!
  • 打赏
  • 举报
回复
hui3zhihui 2004-05-09
是连接部分的问题,你应该把有错误的地方贴出来阿
  • 打赏
  • 举报
回复
killwin 2004-05-09
Access的连接有问题,你连接部分好象没写出来啊?
  • 打赏
  • 举报
回复
wlf427 2004-05-09
这个不是我写的....是书上给的代码.copy下来的
就是不可以用,,,,我是在学习阶段的......
请高手赐教
  • 打赏
  • 举报
回复
qiuyc 2004-05-09
你把那个用别的数据库试试

你用的那个Access本来就不好
  • 打赏
  • 举报
回复
tanyun1111 2004-05-09
我知道,给我加分吧
你的email
我的qq:24324776
  • 打赏
  • 举报
回复
北极猩猩 2004-05-08
应该是你的SQL语句写的有问题吧。
注意,Access对SQL的标准支持的并不完整,有很多东西用不了的
  • 打赏
  • 举报
回复
panpan221 2004-05-08
新手学习
  • 打赏
  • 举报
回复
minghuitian 2004-05-08
gz
  • 打赏
  • 举报
回复
ecaol 2004-05-08
up
  • 打赏
  • 举报
回复
ai92 2004-05-08
GZ
你能不能先自己排除一下
范围太长了
  • 打赏
  • 举报
回复
mxfeng 2004-05-08
晕了~~
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2004-05-08 08:06
社区公告
暂无公告