java运行问题,看好像没问题,

qq_38081432 2017-12-15 11:16:21
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class Dqq {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
JTextArea textArea;
protected String STR,STR_1,STR_2,STR_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dqq window = new Dqq();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Dqq() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

textField = new JTextField();
textField.setBounds(26, 21, 94, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setBounds(135, 21, 85, 21);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setBounds(26, 61, 94, 21);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);

textField_3 = new JTextField();
textField_3.setBounds(134, 61, 86, 21);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);

JButton button = new JButton("\u589E\u52A0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DBConnection s=new DBConnection();
STR=textField.getText().trim();
STR_1=textField_1.getText().trim();
STR_2=textField_2.getText().trim();
STR_3=textField_3.getText().trim();
s.insert(STR,STR_1,STR_2,STR_3); //增加
if(s.i==1)
textArea.append("添加成功");
textArea.append("\n");
}
});
button.setBounds(230, 20, 93, 23);
frame.getContentPane().add(button);

JButton button_1 = new JButton("\u5220\u9664");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DBConnection ss=new DBConnection();
STR_1=textField_1.getText().trim();//删除
ss.delete(STR_1);
if(ss.k==1)
textArea.append("删除成功");
textArea.append("\n");
}
});
button_1.setBounds(331, 20, 93, 23);
frame.getContentPane().add(button_1);

JButton button_2 = new JButton("\u67E5\u627E");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //查找
DBConnection sss=new DBConnection();
Student student=new Student();
STR=textField_1.getText().trim();
sss.select(STR);
textArea.append("年龄"+student.age+"id"+student.id+"信息"+student.message+"名字"+student.name);
textArea.append("\n");
}
});
button_2.setBounds(230, 60, 93, 23);
frame.getContentPane().add(button_2);

JButton button_3 = new JButton("\u66F4\u65B0");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //更新
DBConnection ssss=new DBConnection();
STR=textField.getText().trim();
STR_1=textField_1.getText().trim();
STR_2=textField_2.getText().trim();
STR_3=textField_3.getText().trim();
ssss.update(STR,STR_1,STR_2,STR_3);
if(ssss.j==1)
textArea.append("更新成功");
textArea.append("\n");
}
});
button_3.setBounds(331, 60, 93, 23);
frame.getContentPane().add(button_3);

JTextArea textArea = new JTextArea();
textArea.setBounds(26, 124, 290, 128);
frame.getContentPane().add(textArea);
}

}


public class DBConnection {
private String dbDriver="com.mysql.jdbc.Driver";
private String dbUrl="jdbc:mysql://localhost:3306/student";
private String dbUser="root";
private String dbPass="123456789";
int i=0 ;
int j=0;
int k=0;
int l=0;
public Connection getConn()
{
Connection conn=null;
try
{
Class.forName(dbDriver);
System.out.println("JBDC 加载成功!");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public int insert(String a,String b,String c,String d) //增加
{

String sql="insert into test values(?,?,?,?)";
Connection cnn=getConn();
try{
PreparedStatement preStmt =cnn.prepareStatement(sql);
preStmt.setString(1,a);
preStmt.setString(1,b);
preStmt.setString(1,c);
preStmt.setString(1,d);
i=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return i;
}
public int update(String a,String b,String c,String d)
{
String sql="update test set age=?,message=? where name=c";
Connection cnn=getConn();
try{
PreparedStatement preStmt =cnn.prepareStatement(sql);
preStmt.setString(1,a);
preStmt.setString(2,c);
j=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return j;
}
public int delete(String c)
{
String sql = "delete from test where name=a";
Connection conn = getConn();
try
{
java.sql.Statement stmt = conn.createStatement();
k = stmt.executeUpdate(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return k;
}
public int select(String c) //查找
{
Student Stu=new Student();
String sql = "select * from test where name=a";
try
{
Connection conn = getConn();
java.sql.Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);

if(rs.next())
{
Stu.age = rs.getString(1);//
Stu.id= rs.getString(2);
Stu.message= rs.getString(3);
Stu.name= rs.getString(4);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return l;
}
}
...全文
686 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
钢琴上的代码 2017-12-18
  • 打赏
  • 举报
回复
查找的结果是null,说明没查到,你确定数据库里有你要查的数据,直接用语句去看一下.
qq_34350475 2017-12-18
  • 打赏
  • 举报
回复
引用 17 楼 qq_38081432 的回复:
[quote=引用 16 楼 qq_34350475 的回复:] 先单独运行下sql语句看可不可以执行,报错提示是where里的列名C无法识别,是不是列名映射错了,跟你数据库里的名字对不上
那个识别改好了,还剩下 那个查找的输出都是null,那个更新不了[/quote] 先写死sql查询语句,比如“select * from Table where age=20”,确定你整个流程走得通,如果可以了的话,那很大原因就是你参数传的有问题,字符串拼接多“”或者少了“”的问题
shenhaoliang 2017-12-16
  • 打赏
  • 举报
回复
没什么问题呀
qq_34350475 2017-12-16
  • 打赏
  • 举报
回复
先单独运行下sql语句看可不可以执行,报错提示是where里的列名C无法识别,是不是列名映射错了,跟你数据库里的名字对不上
qq_38081432 2017-12-16
  • 打赏
  • 举报
回复
引用 16 楼 qq_34350475 的回复:
先单独运行下sql语句看可不可以执行,报错提示是where里的列名C无法识别,是不是列名映射错了,跟你数据库里的名字对不上

那个识别改好了,还剩下
那个查找的输出都是null,那个更新不了
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
直接运行没显示有错误~
自由自在_Yu 2017-12-15
  • 打赏
  • 举报
回复
错误信息呢?这么一大串代码,没有重点,不好查
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 8 楼 qq_38081432 的回复:
[quote=引用 7 楼 mmqw1122 的回复:] [quote=引用 4 楼 mmqw1122 的回复:] 有什么问题啊
那就是逻辑问题,你至少说下要干嘛,要出什么结果啊[/quote] [/quote] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'c' in 'where clause' 运行报错 Unknown column 'c' in 'where clause' 这几个update,delete,select都是这样 我觉得是这几段代码问题 public int update(String a,String b,String c,String d) public int delete(String c) public int select(String c) 都是把名字传过去的
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 12 楼 lye2000000_super 的回复:
然后出现了什么问题呢?你单步调试一下,看看哪里有问题。。。。是否报错。。。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'c' in 'where clause' 运行报错 Unknown column 'c' in 'where clause' 这几个update,delete,select都是这样 我觉得是这几段代码问题 public int update(String a,String b,String c,String d) public int delete(String c) public int select(String c) 都是把名字传过去的
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 1 楼 yuxiangaaaaa 的回复:
错误信息呢?这么一大串代码,没有重点,不好查
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'c' in 'where clause' 运行报错 Unknown column 'c' in 'where clause' 这几个update,delete,select都是这样 我觉得是这几段代码问题 public int update(String a,String b,String c,String d) public int delete(String c) public int select(String c) 都是把名字传过去的
  • 打赏
  • 举报
回复
然后出现了什么问题呢?你单步调试一下,看看哪里有问题。。。。是否报错。。。
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
Dqq类,是创建组件以及相关事件处理; DBC类是连接mysql数据库和操作mysql数据库
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 9 楼 lye2000000_super 的回复:
还是没明白你要干什么?你的功能是什么?
public int insert(String a,String b,String c,String d) //增加 { String sql="insert into test values(?,?,?,?)"; Connection cnn=getConn(); try{ PreparedStatement preStmt =cnn.prepareStatement(sql); preStmt.setString(1,a); preStmt.setString(1,b); preStmt.setString(1,c); preStmt.setString(1,d); i=preStmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return i; } 更新 public int update(String a,String b,String c,String d) { String sql="update test set age=?,message=? where name=c"; Connection cnn=getConn(); try{ PreparedStatement preStmt =cnn.prepareStatement(sql); preStmt.setString(1,a); preStmt.setString(2,c); j=preStmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return j; } 删除 public int delete(String c) { String sql = "delete from test where name=a"; Connection conn = getConn(); try { java.sql.Statement stmt = conn.createStatement(); k = stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return k; } public int select(String c) //查找 { Student Stu=new Student(); String sql = "select * from test where name=a"; try { Connection conn = getConn(); java.sql.Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); if(rs.next()) { Stu.age = rs.getString(1);// Stu.id= rs.getString(2); Stu.message= rs.getString(3); Stu.name= rs.getString(4); } } catch (SQLException e) { e.printStackTrace(); } return l; } } 这几段是功能
  • 打赏
  • 举报
回复
还是没明白你要干什么?你的功能是什么?
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 7 楼 mmqw1122 的回复:
[quote=引用 4 楼 mmqw1122 的回复:]
有什么问题啊


那就是逻辑问题,你至少说下要干嘛,要出什么结果啊[/quote]

mmqw 2017-12-15
  • 打赏
  • 举报
回复
引用 4 楼 mmqw1122 的回复:
有什么问题啊
那就是逻辑问题,你至少说下要干嘛,要出什么结果啊
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 3 楼 lye2000000_super 的回复:
没问题就是正确的啊。。。这是要提问什么?是分享?
功能没法用~~
qq_38081432 2017-12-15
  • 打赏
  • 举报
回复
引用 4 楼 mmqw1122 的回复:
有什么问题啊
功能没法用
mmqw 2017-12-15
  • 打赏
  • 举报
回复
有什么问题啊
  • 打赏
  • 举报
回复
没问题就是正确的啊。。。这是要提问什么?是分享?

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧