我使用prepareStatement接口存储,但运行后没有存进,我不知道代码错在哪里。附上代码和数据库(使用SQL2000的数据库)。以下是数据库的结构:create database gouwuuse gouwucreate table member
(
mId int primary key identity(1,1),
mName varchar(20) not null,
mPd varchar(20) not null,
mSex varchar(8) check(mSex='男'or mSex='女'),
mAge int not null,
mLike varchar(20) not null,
mCity varchar(20) not null,
mPresent varchar(200) not null,
)
注册源代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.awt.event.ActionListener;
public class rogin extends JFrame implements ActionListener {
static JPanel jp,jp1,jp2,jp3,jp4,jp5,jp6,jp7;
static JLabel jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8;
static JTextField jtxt1,jtxt2,jtxt3;
static JTextArea jta;
static JPasswordField jpf1,jpf2;
static JRadioButton jrb1,jrb2;
static JComboBox jbb;
static JScrollPane jsp;
static String s[]={"北京","天津","上海","广州","深圳"};
static JButton jbn;
static ButtonGroup bgroup;
public rogin()
{
super("用户注册");
jp1=new JPanel();
jp1.setLayout(new GridLayout(3,3));
jb1=new JLabel("用 户 名: ");
jtxt1=new JTextField(10);
jtxt1.setToolTipText("用户名不能为空");
jp1.add(jb1);
jp1.add(jtxt1);
jb2=new JLabel("设 置 密 码: ");
jpf1=new JPasswordField(10);
jpf1.setEchoChar('*');
jb3=new JLabel("确 认 密 码: ");
jpf2=new JPasswordField(10);
jpf2.setEchoChar('*');
jp1.add(jb2);
jp1.add(jpf1);
jp1.add(jb3);
jp1.add(jpf2);
jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.CENTER));
jb4=new JLabel("性 别: ");
jrb1=new JRadioButton("男" );
jrb2=new JRadioButton("女" );
bgroup=new ButtonGroup();
bgroup.add(jrb1);
bgroup.add(jrb2);
jp2.add(jb4);
jp2.add(jrb1);
jp2.add(jrb2);
jp3=new JPanel();
jp3.setLayout(new GridLayout(1,1));
jb5=new JLabel("年 龄: ");
jtxt2=new JTextField(4);
jp3.add(jb5);
jp3.add(jtxt2);
jp4=new JPanel();
jb6=new JLabel("爱好:");
jtxt3=new JTextField(10);
jp4.add(jb6);
jp4.add(jtxt3);
jp5=new JPanel();
jb7=new JLabel("城市:");
jbb=new JComboBox(s);
jp5.add(jb7);
jp5.add(jbb);
jp6=new JPanel();
jb8=new JLabel("个人介绍:");
jta=new JTextArea(10,20);
jsp=new JScrollPane(jta);
jta.setEditable(true);
jp6.add(jb8);
jp6.add(jsp);
jp7=new JPanel();
jbn=new JButton("注册");
jp7.add(jbn);
jp=new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
jp.add(jp4);
jp.add(jp5);
jp.add(jp6);
jp.add(jp7);
jbn.addActionListener(this);
this.add(jp);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(350, 600);
this.setVisible(true);
}
public static void main(String[] args) {
new rogin();
}
class sql
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement Pstmt=null;
sql()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mysql","sa","");
stmt=conn.createStatement();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent arg0) {
JButton jtn1=(JButton)arg0.getSource();
if(jtn1.getText().equals("注册"))
{
sql b=new sql();
try {
b.Pstmt=b.conn.prepareStatement("insert into member values(?,?,?,?,?,?,?)");
b.Pstmt.setString(1, rogin.jtxt1.getText());
b.Pstmt.setString(2,rogin.jpf1.getText());
b.Pstmt.setString(3, rogin.bgroup.toString());
b.Pstmt.setInt(4,Integer.parseInt(rogin.jtxt2.getText()));
b.Pstmt.setString(5, rogin.jtxt3.getText());
b.Pstmt.setInt(6, rogin.jbb.getSelectedIndex());
b.Pstmt.setString(7, rogin.jta.getText());
b.Pstmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
int n=JOptionPane.showConfirmDialog(null, "恭喜你注册成功!是否现在登陆","信息提示",JOptionPane.YES_NO_OPTION);
if(n==0)
{
new login();
this.dispose();
}
if(n==1)
{
this.dispose();
}
}
}
}
望高手帮助修正,急急!!!!
(
mId int primary key identity(1,1),
mName varchar(20) not null,
mPd varchar(20) not null,
mSex varchar(8) check(mSex='男'or mSex='女'),
mAge int not null,
mLike varchar(20) not null,
mCity varchar(20) not null,
mPresent varchar(200) not null,
)
注册源代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.awt.event.ActionListener;
public class rogin extends JFrame implements ActionListener {
static JPanel jp,jp1,jp2,jp3,jp4,jp5,jp6,jp7;
static JLabel jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8;
static JTextField jtxt1,jtxt2,jtxt3;
static JTextArea jta;
static JPasswordField jpf1,jpf2;
static JRadioButton jrb1,jrb2;
static JComboBox jbb;
static JScrollPane jsp;
static String s[]={"北京","天津","上海","广州","深圳"};
static JButton jbn;
static ButtonGroup bgroup;
public rogin()
{
super("用户注册");
jp1=new JPanel();
jp1.setLayout(new GridLayout(3,3));
jb1=new JLabel("用 户 名: ");
jtxt1=new JTextField(10);
jtxt1.setToolTipText("用户名不能为空");
jp1.add(jb1);
jp1.add(jtxt1);
jb2=new JLabel("设 置 密 码: ");
jpf1=new JPasswordField(10);
jpf1.setEchoChar('*');
jb3=new JLabel("确 认 密 码: ");
jpf2=new JPasswordField(10);
jpf2.setEchoChar('*');
jp1.add(jb2);
jp1.add(jpf1);
jp1.add(jb3);
jp1.add(jpf2);
jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.CENTER));
jb4=new JLabel("性 别: ");
jrb1=new JRadioButton("男" );
jrb2=new JRadioButton("女" );
bgroup=new ButtonGroup();
bgroup.add(jrb1);
bgroup.add(jrb2);
jp2.add(jb4);
jp2.add(jrb1);
jp2.add(jrb2);
jp3=new JPanel();
jp3.setLayout(new GridLayout(1,1));
jb5=new JLabel("年 龄: ");
jtxt2=new JTextField(4);
jp3.add(jb5);
jp3.add(jtxt2);
jp4=new JPanel();
jb6=new JLabel("爱好:");
jtxt3=new JTextField(10);
jp4.add(jb6);
jp4.add(jtxt3);
jp5=new JPanel();
jb7=new JLabel("城市:");
jbb=new JComboBox(s);
jp5.add(jb7);
jp5.add(jbb);
jp6=new JPanel();
jb8=new JLabel("个人介绍:");
jta=new JTextArea(10,20);
jsp=new JScrollPane(jta);
jta.setEditable(true);
jp6.add(jb8);
jp6.add(jsp);
jp7=new JPanel();
jbn=new JButton("注册");
jp7.add(jbn);
jp=new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
jp.add(jp4);
jp.add(jp5);
jp.add(jp6);
jp.add(jp7);
jbn.addActionListener(this);
this.add(jp);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(350, 600);
this.setVisible(true);
}
public static void main(String[] args) {
new rogin();
}
class sql
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement Pstmt=null;
sql()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mysql","sa","");
stmt=conn.createStatement();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent arg0) {
JButton jtn1=(JButton)arg0.getSource();
if(jtn1.getText().equals("注册"))
{
sql b=new sql();
try {
b.Pstmt=b.conn.prepareStatement("insert into member values(?,?,?,?,?,?,?)");
b.Pstmt.setString(1, rogin.jtxt1.getText());
b.Pstmt.setString(2,rogin.jpf1.getText());
b.Pstmt.setString(3, rogin.bgroup.toString());
b.Pstmt.setInt(4,Integer.parseInt(rogin.jtxt2.getText()));
b.Pstmt.setString(5, rogin.jtxt3.getText());
b.Pstmt.setInt(6, rogin.jbb.getSelectedIndex());
b.Pstmt.setString(7, rogin.jta.getText());
b.Pstmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
int n=JOptionPane.showConfirmDialog(null, "恭喜你注册成功!是否现在登陆","信息提示",JOptionPane.YES_NO_OPTION);
if(n==0)
{
new login();
this.dispose();
}
if(n==1)
{
this.dispose();
}
}
}
}
望高手帮助修正,急急!!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货