本人新手一个 在做一个系统,要实现增删改查的功能,但是在修改的时候遇到了困难,求哪位大侠帮忙指导下。下面是我的代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Xg extends JFrame{
JTextField jtf1,jtf2,jtf3,jtf0;
JLabel jl1,jl2,jl3,jl0;
JButton jb1;
static Statement st;
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test?"+"user=root&password=root";
Connection conn=DriverManager.getConnection(url);
st=conn.createStatement();
}
catch(Exception e){}
}
Xg()
{JButton jb1=new JButton();
jtf0=new JTextField(15);
jtf0.setText("序列号为修改依据");
jtf1=new JTextField(15);
jtf2=new JTextField(15);
jtf3=new JTextField(15);
jl3=new JLabel("学号");
jl1=new JLabel("姓名");
jl2=new JLabel("身份");
jl0=new JLabel("成绩");
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(4,2));
jb1.addActionListener(new mysql());
jp1.add(jl0); jp1.add(jtf0);
jp1.add(jl1); jp1.add(jtf1);
jp1.add(jl2); jp1.add(jtf2);
jp1.add(jl3); jp1.add(jtf3);
jp1.add(jb1);
Container container=getContentPane();
container.add(jp1,BorderLayout.CENTER );
container.add(jb1, BorderLayout.SOUTH);
setTitle("更新数据窗口");
setSize(350,200);
setVisible(true);
}
public class mysql implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
String str1=jtf1.getText().trim();
String str2=jtf2.getText().trim();
String str3=jtf3.getText().trim();
String str0=jtf0.getText().trim();
if(str1.equals("")|str2.equals("")|str3.equals("")|str0.equals("")){
JOptionPane.showMessageDialog(Xg.this, "请重新输入学生信息","提示对话框",1);
}
else {
int n=Integer.parseInt(str0);
String sql="update student set v_name="+str1+",id="+str2+",xuehao="+str3+" where="+n+"";
st.executeUpdate(sql);
JOptionPane.showMessageDialog(Xg.this, "数据修改成功","提示对话框",1);
jtf0.setText("");jtf1.setText("");jtf2.setText("");jtf3.setText("");
}
}
catch(Exception ee){}
}
}
public static void main(String []args){
new Xg();
}
}
这下面是我的表
v_name id xuehao chengji
xiaoming 1 5 334
------解决方案--------------------
给你改了一下,可以执行update了,但是里面问题依然还很多,也不知道你数据表编码,可能会有中文乱码问题,还有插入成功后对话框应该关闭的……
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.sql.*;public class Xg extends JFrame{ JTextField jtf1,jtf2,jtf3,jtf4,jtf0; JLabel jl1,jl2,jl3,jl4,jl0; JButton jb1; static Statement st; static { try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/test?"+"user=root&password=root"; Connection conn=DriverManager.getConnection(url); st=conn.createStatement(); } catch(Exception e){} } public Xg(){ JButton jb1=new JButton("提交"); jtf0=new JTextField(15); jtf0.setText("序列号为修改依据"); jtf1=new JTextField(15); jtf2=new JTextField(15); jtf3=new JTextField(15); jtf4=new JTextField(15); jl3=new JLabel("学号"); jl1=new JLabel("姓名"); jl2=new JLabel("身份"); jl4=new JLabel("成绩"); jl0=new JLabel("序列号"); JPanel jp1=new JPanel(); jp1.setLayout(new GridLayout(5,2)); jb1.addActionListener(new Mysql()); jp1.add(jl0); jp1.add(jtf0); jp1.add(jl1); jp1.add(jtf1); jp1.add(jl2); jp1.add(jtf2); jp1.add(jl3); jp1.add(jtf3); jp1.add(jl4); jp1.add(jtf4); jp1.add(jb1); Container container=getContentPane(); container.add(jp1,BorderLayout.CENTER ); container.add(jb1, BorderLayout.SOUTH); setTitle("更新数据窗口"); setSize(350,200); setVisible(true); } public class Mysql implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("-------------"); try { String str1=jtf1.getText().trim(); String str2=jtf2.getText().trim(); String str3=jtf3.getText().trim(); String str4=jtf4.getText().trim(); String str0=jtf0.getText().trim(); if(str1.equals("")|str2.equals("")|str3.equals("")|str0.equals("")){ JOptionPane.showMessageDialog(Xg.this, "请重新输入学生信息","提示对话框",1); } else { int n=Integer.parseInt(str0); //假如成绩这列是int类型,如果不是就会报错 int chengji=Integer.parseInt(str4); String sql="update student2 set v_name="+str1+",id="+str2+",xuehao="+str3+ ",chengji="+chengji+" where id="+n+""; st.executeUpdate(sql); System.out.println("执行SQL语句"); JOptionPane.showMessageDialog(Xg.this, "数据修改成功","提示对话框",1); jtf0.setText("");jtf1.setText("");jtf2.setText("");jtf3.setText(""); } } catch(Exception ee){} } } public static void main(String []args){ new Xg(); }}