首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

不明白为啥错了,请大家指导解决思路

2012-01-21 
不明白为啥错了,请大家指导//只是数据库建的表(sql server 2000)create table stu( stuId varchar(30) pri

不明白为啥错了,请大家指导
//只是数据库建的表(sql server 2000)

create table stu
( stuId varchar(30) primary key,
  stuName nvarchar(50) not null,
  stuSex nchar(1) check (stuSex in('男','女') )default '男',
  stuAge int check (stuAge>1) ,
  stuJg nvarchar(30),
  stuDept nvarchar(40)
)
 insert into stu values('sp001','孙悟空','男',20,'花果山','少林派');
 insert into stu values('sp002','猪八戒','男',40,'高老庄','天上派');
 insert into stu values('sp003','沙悟净','男',30,'花果山','少林派');
 insert into stu values('sp004','唐三藏','男',20,'花果山','少林派');

select * from stu //

下面是在java中写的代码


package com.zkyc;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.accessibility.Accessible;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Test extends JFrame implements ActionListener{
 
//

JTable jt = null;
JScrollPane jsp = null;
JPanel jp1;
JPanel jp2;
JLabel jl = null;
JTextField jtf = null;
JButton jb1, jb2,jb3,jb4;

public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();

}

@SuppressWarnings("unchecked")
public Test(){

StuModel sm = new StuModel();

jt = new JTable(sm.rowData, sm.columnNames);
jsp = new JScrollPane(jt);
this.add(jsp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


jp1 = new JPanel();
jtf = new JTextField(10);
jl = new JLabel("请输入查询姓名 ");
jb1 = new JButton("查询");
jb1.addActionListener(this);
jp1.add(jl);
jp1.add(jtf);
jp1.add(jb1);
//this.add(jp1, BorderLayout.NORTH);
//this.add(jt, BorderLayout.CENTER);
jp2 = new JPanel();
jb2 = new JButton("添加");
jb2.addActionListener(this);
jb3 = new JButton("修改");
jb4 = new JButton("删除");
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
this.setLocation(300, 300);
this.add(jp1,BorderLayout.NORTH);
this.add(jp2, BorderLayout.SOUTH);
this.add(jt, BorderLayout.CENTER);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jb1) {
String name = this.jtf.getText().trim();
String sql = "select * from stu where stuName='"+name+"'";
//构建新的数据模型类,并更新
StuModel sm = new StuModel(sql);
//跟心Jtable
jt.setModel(sm);
}
else if (e.getSource() == jb2) {
new StuDialog();

StuModel sm = new StuModel();
//跟心Jtable
jt.setModel(sm);
}

}





}







package com.zkyc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class StuModel extends AbstractTableModel{

Vector rowData, columnNames;
//定义操作数据库所学要的
PreparedStatement ps = null;
ResultSet rs = null;
Connection ct = null;

public void init (String sql){

if (sql.equalsIgnoreCase("")) {


sql="select * from stu";
}
columnNames = new Vector();
Vector lieVector = new Vector();
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
//lieVector.add(columnNames);

rowData = new Vector();
//Vector hang = new Vector();
//hang.add("sp001");
//hang.add("孙悟空");
//hang.add("男");
//hang.add("20");
//hang.add("花果山");
//hang.add("少林派");
//rowData.add(hang);
//加载驱动
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
try {
ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=master","sa","cao");
 
ps = ct.prepareStatement(sql);
rs = ps.executeQuery();

while (rs.next()) {

Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
rowData.add(hang);


}


catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ct != null) {
try {
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}


//通过传递的sql语句来获得数据模型
public StuModel(String sql){

this.init(sql);
}


public StuModel(){
this.init("");
}

@Override
public String getColumnName(int arg0) {
// TODO Auto-generated method stub
return (String) this.columnNames.get(arg0);
}

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}

@Override
//得到某行和某列的数据
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return ((Vector)this.rowData.get(arg0)).get(arg1);
}

}










package com.zkyc;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.microsoft.jdbc.sqlserver.SQLServerColumn;

public class StuDialog extends JDialog implements ActionListener{



JLabel jl1, jl2,jl3,jl4,jl5,jl6;
JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
JButton jb1, jb2;
JPanel jp1, jp2,jp3;

public StuDialog(){

jl1 = new JLabel("学号");
jl2 = new JLabel("姓名");
jl3 = new JLabel("性别");
jl4 = new JLabel("年龄");
jl5 = new JLabel("籍贯");
jl6 = new JLabel("系别");

jtf1 = new JTextField();
jtf2 = new JTextField(21);
jtf3 = new JTextField(21);
jtf4 = new JTextField(21);
jtf5 = new JTextField(21);
jtf6 = new JTextField(21);

jb1 = new JButton("添加");
jb1.addActionListener(this);
jb2 = new JButton("取消");

jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
 
jp1.setLayout(new GridLayout(6, 1));
jp2.setLayout(new GridLayout(6, 1));
 
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
 
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
 

jp3.add(jb1);
jp3.add(jb2);

this.add(jp1, BorderLayout.WEST);
this.add(jp2,BorderLayout.EAST);
this.add(jp3, BorderLayout.SOUTH);
this.setLocation(200, 200);
this.setSize(300,200);
pack();
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {

PreparedStatement pstmt = null;
ResultSet rs ;
Connection conn = null;
//Statement stmt = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=master";


conn = DriverManager.getConnection(url,"sa","cao");

String sql = "insert into stu values (?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
//rs = pstmt.executeUpdate();

pstmt.setString(1, jtf1.getText());
pstmt.setString(2, jtf1.getText());
pstmt.setString(3, jtf1.getText());
pstmt.setString(4, jtf1.getText());
pstmt.setString(5, jtf1.getText());
pstmt.setString(6, jtf1.getText());

pstmt.executeUpdate();

//关闭对话框
this.dispose();

} catch (Exception e2) {
e2.printStackTrace();
}finally{
if(pstmt != null)
try {
pstmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//if(stmt!=null) 
if(conn != null)
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

}


}


[解决办法]
什么错?

Java code
pstmt.setString(1, jtf1.getText());pstmt.setString(2, jtf2.getText());pstmt.setString(3, jtf3.getText());pstmt.setString(4, jtf4.getText());pstmt.setString(5, jtf5.getText());pstmt.setString(6, jtf6.getText());
[解决办法]
好长,问题没有描述啊
[解决办法]
是啊
探讨

好长,问题没有描述啊

[解决办法]
代码看得我晕,啥错误。。。
------解决方案--------------------


楼主想问什么啊
[解决办法]
无语啊 上来就PO 一段长长的代码 错误也没说 汗死
[解决办法]
那个长阿,,
[解决办法]
pstmt.setInt(4, Integer.parseInt(jtf1.getText()));
pstmt.setString(5, jtf1.getText());
pstmt.setString(6, jtf1.getText());
[解决办法]
那也许就是你将字符串转换为int时有问题了,错误码是什么?

热点排行