Java连接数据库 java帮助 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 仔细看了一遍,得出三个结论,首先楼主使用的是java自带的java db数据库,然后楼主使用的是很原始的数据库驱动,最后楼主的sql语句可能有问题。建议使用derby.jar的驱动类。然后建议使用这个借口的方法:PreparedStatement pst = conn.prepareStatement(sql);因为原先的接口会存在sql注入 还有个问题,请注意代码格式化,请不要把异常信息用catch(SQLException ee) {System.out.println("sjbjf");}这个东西给代替了,不然连问题在哪都不知道,请注意检查executeUpdate返回行数是不是大于0 package demo;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Baidu extends JFrame implements ActionListener { PreparedStatement sm = null; Connection ct = null; ResultSet rs = null; JLabel[] bq = { new JLabel("工号"), new JLabel("姓名"), new JLabel("工资"), new JLabel("负责线路"), new JLabel("工作类型") };// JTextField[] t = { new JTextField(""), new JTextField(""), new JTextField(""), new JTextField(""), new JTextField(""), new JTextField("") };// 输入信息框 JLabel l0; JPanel p1, p2; JButton an1, an2;// 确定员工信息及返回上一页按钮 public Baidu() { p1 = new JPanel(); p1.setLayout(null); l0 = new JLabel("欢迎使用添加员工信息界面"); l0.setFont(new Font("宋体", Font.PLAIN, 26)); l0.setBounds(100, 0, 350, 40); // 添加标签及文本框 for (int i = 0; i < bq.length; i++) { if (i == 4 || i == 3) { bq[i].setFont(new Font("宋体", Font.PLAIN, 26)); bq[i].setBounds(70, 80 + i * 70, 130, 26); } else { bq[i].setBounds(120, 80 + i * 70, 130, 28); bq[i].setFont(new Font("宋体", Font.PLAIN, 26)); } t[i].setBounds(220, 80 + i * 70, 130, 26); t[i].setFont(new Font("宋体", Font.PLAIN, 26)); p1.add(t[i]); p1.add(bq[i]); // bq[i].addActionListener(this); } // 添加确定按钮 an1 = new JButton("确定"); an1.setBounds(100, 420, 100, 30); an1.setFont(new Font("宋体", Font.PLAIN, 26)); an1.setForeground(Color.blue); p1.add(an1); an1.addActionListener(this); // 添加返回按钮 an2 = new JButton("返回"); an2.setBounds(260, 420, 100, 30); an2.setFont(new Font("宋体", Font.PLAIN, 26)); an2.setForeground(Color.blue); p1.add(an2); an2.addActionListener(this); // 提示信息 p1.add(l0); this.add(p1); this.setTitle("添加工作人员信息"); this.setBounds(100, 100, 500, 600); this.setResizable(false); this.setResizable(false);// 固定界面大小,不允许最大化 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); try { Class.forName("com.mysql.jdbc.Driver"); ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名","root","密码");//修改数据库名,还有密码 } catch (Exception e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == an1) { if (t[0].getText().isEmpty() || t[1].getText().isEmpty() || t[2].getText().isEmpty() || t[3].getText().isEmpty() || t[4].getText().isEmpty()) { JOptionPane.showMessageDialog(this, "文本框不能为空!!!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); return; } else { try { String u0 = t[0].getText(); String u1 = t[1].getText(); String u2 = t[2].getText(); String u3 = t[3].getText(); String u4 = t[4].getText(); String sql = "insert into employee(number,name,salary,num,work)" + " values ('" + u0 + "','" + u1 + "','" + u2 + "','" + u3 + "','" + u4 + "')"; sm = ct.prepareStatement(sql); int a=sm.executeUpdate(sql); if(a>0){ JOptionPane.showMessageDialog(this, "添加成功!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(this, "对不起!添加失败!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); } sm.close(); } catch (SQLException ee) { ee.printStackTrace(); } } } } public static void main(String[] args) { Baidu in = new Baidu(); }} package demo;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Baidu extends JFrame implements ActionListener { PreparedStatement sm = null; Connection ct = null; ResultSet rs = null; JLabel[] bq = { new JLabel("工号"), new JLabel("姓名"), new JLabel("工资"), new JLabel("负责线路"), new JLabel("工作类型") };// JTextField[] t = { new JTextField(""), new JTextField(""), new JTextField(""), new JTextField(""), new JTextField(""), new JTextField("") };// 输入信息框 JLabel l0; JPanel p1, p2; JButton an1, an2;// 确定员工信息及返回上一页按钮 public Baidu() { p1 = new JPanel(); p1.setLayout(null); l0 = new JLabel("欢迎使用添加员工信息界面"); l0.setFont(new Font("宋体", Font.PLAIN, 26)); l0.setBounds(100, 0, 350, 40); // 添加标签及文本框 for (int i = 0; i < bq.length; i++) { if (i == 4 || i == 3) { bq[i].setFont(new Font("宋体", Font.PLAIN, 26)); bq[i].setBounds(70, 80 + i * 70, 130, 26); } else { bq[i].setBounds(120, 80 + i * 70, 130, 28); bq[i].setFont(new Font("宋体", Font.PLAIN, 26)); } t[i].setBounds(220, 80 + i * 70, 130, 26); t[i].setFont(new Font("宋体", Font.PLAIN, 26)); p1.add(t[i]); p1.add(bq[i]); // bq[i].addActionListener(this); } // 添加确定按钮 an1 = new JButton("确定"); an1.setBounds(100, 420, 100, 30); an1.setFont(new Font("宋体", Font.PLAIN, 26)); an1.setForeground(Color.blue); p1.add(an1); an1.addActionListener(this); // 添加返回按钮 an2 = new JButton("返回"); an2.setBounds(260, 420, 100, 30); an2.setFont(new Font("宋体", Font.PLAIN, 26)); an2.setForeground(Color.blue); p1.add(an2); an2.addActionListener(this); // 提示信息 p1.add(l0); this.add(p1); this.setTitle("添加工作人员信息"); this.setBounds(100, 100, 500, 600); this.setResizable(false); this.setResizable(false);// 固定界面大小,不允许最大化 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); try { Class.forName("com.mysql.jdbc.Driver"); ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","lulu1101");//修改数据库名,还有密码 } catch (Exception e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == an1) { if (t[0].getText().isEmpty() || t[1].getText().isEmpty() || t[2].getText().isEmpty() || t[3].getText().isEmpty() || t[4].getText().isEmpty()) { JOptionPane.showMessageDialog(this, "文本框不能为空!!!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); return; } else { try { String u0 = t[0].getText(); String u1 = t[1].getText(); String u2 = t[2].getText(); String u3 = t[3].getText(); String u4 = t[4].getText(); String sql = "insert into employee(number,name,salary,num,work)" + " values ('" + u0 + "','" + u1 + "','" + u2 + "','" + u3 + "','" + u4 + "')"; sm = ct.prepareStatement(sql); int a=sm.executeUpdate(sql); if(a>0){ JOptionPane.showMessageDialog(this, "添加成功!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); for(int i=0;i<t.length;i++){ JTextField jf=t[i]; jf.setText(""); } }else{ JOptionPane.showMessageDialog(this, "对不起!添加失败!", "消息对话框!", JOptionPane.INFORMATION_MESSAGE); } sm.close(); } catch (SQLException ee) { ee.printStackTrace(); } } } } public static void main(String[] args) { Baidu in = new Baidu(); }} 这代码太凌乱,楼主是用 java 自带的数据库?没看见你代码有加载数据库驱动啊 菜鸟求助,望好心人帮看看 <? extends T> 和 <? super T>分别是什么意思?有什么不同? 如何用编码方式忽略 CTRL_LOGOFF_EVENT? java -jar 使用提问 请问各位,HashSet不能直接new对象吗?帮我看看这个程序哪里有问题 关于类对象.! JAVA6个基础题,希望有人详细讲解下. java如何调用Lib文件 java 操作数据库通用类 求助:为什么在jb7中的数据库我只能在第一次新建的时候能够改动,以后进去后不能改动? 这个怎么做啊,Servlet里面service方法不能写xml的代码运行无效啊 java 接口中再定义接口 算怎么回事
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class Baidu extends JFrame implements ActionListener {
PreparedStatement sm = null;
Connection ct = null;
ResultSet rs = null;
JLabel[] bq = { new JLabel("工号"), new JLabel("姓名"), new JLabel("工资"),
new JLabel("负责线路"), new JLabel("工作类型") };//
JTextField[] t = { new JTextField(""), new JTextField(""),
new JTextField(""), new JTextField(""), new JTextField(""),
new JTextField("") };// 输入信息框
JLabel l0;
JPanel p1, p2;
JButton an1, an2;// 确定员工信息及返回上一页按钮 public Baidu() {
p1 = new JPanel();
p1.setLayout(null);
l0 = new JLabel("欢迎使用添加员工信息界面");
l0.setFont(new Font("宋体", Font.PLAIN, 26));
l0.setBounds(100, 0, 350, 40); // 添加标签及文本框
for (int i = 0; i < bq.length; i++) {
if (i == 4 || i == 3) {
bq[i].setFont(new Font("宋体", Font.PLAIN, 26));
bq[i].setBounds(70, 80 + i * 70, 130, 26);
} else {
bq[i].setBounds(120, 80 + i * 70, 130, 28);
bq[i].setFont(new Font("宋体", Font.PLAIN, 26));
}
t[i].setBounds(220, 80 + i * 70, 130, 26);
t[i].setFont(new Font("宋体", Font.PLAIN, 26));
p1.add(t[i]);
p1.add(bq[i]);
// bq[i].addActionListener(this);
}
// 添加确定按钮
an1 = new JButton("确定");
an1.setBounds(100, 420, 100, 30);
an1.setFont(new Font("宋体", Font.PLAIN, 26));
an1.setForeground(Color.blue);
p1.add(an1);
an1.addActionListener(this);
// 添加返回按钮
an2 = new JButton("返回");
an2.setBounds(260, 420, 100, 30);
an2.setFont(new Font("宋体", Font.PLAIN, 26));
an2.setForeground(Color.blue);
p1.add(an2);
an2.addActionListener(this); // 提示信息 p1.add(l0);
this.add(p1);
this.setTitle("添加工作人员信息");
this.setBounds(100, 100, 500, 600);
this.setResizable(false);
this.setResizable(false);// 固定界面大小,不允许最大化
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名","root","密码");//修改数据库名,还有密码
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == an1) {
if (t[0].getText().isEmpty() || t[1].getText().isEmpty()
|| t[2].getText().isEmpty() || t[3].getText().isEmpty()
|| t[4].getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "文本框不能为空!!!", "消息对话框!",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
try {
String u0 = t[0].getText();
String u1 = t[1].getText();
String u2 = t[2].getText();
String u3 = t[3].getText();
String u4 = t[4].getText();
String sql = "insert into employee(number,name,salary,num,work)"
+ " values ('"
+ u0
+ "','"
+ u1
+ "','"
+ u2
+ "','" + u3 + "','" + u4 + "')";
sm = ct.prepareStatement(sql);
int a=sm.executeUpdate(sql);
if(a>0){
JOptionPane.showMessageDialog(this, "添加成功!",
"消息对话框!", JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(this, "对不起!添加失败!",
"消息对话框!", JOptionPane.INFORMATION_MESSAGE);
}
sm.close();
} catch (SQLException ee) {
ee.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Baidu in = new Baidu();
}}
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class Baidu extends JFrame implements ActionListener {
PreparedStatement sm = null;
Connection ct = null;
ResultSet rs = null;
JLabel[] bq = { new JLabel("工号"), new JLabel("姓名"), new JLabel("工资"),
new JLabel("负责线路"), new JLabel("工作类型") };//
JTextField[] t = { new JTextField(""), new JTextField(""),
new JTextField(""), new JTextField(""), new JTextField(""),
new JTextField("") };// 输入信息框
JLabel l0;
JPanel p1, p2;
JButton an1, an2;// 确定员工信息及返回上一页按钮 public Baidu() {
p1 = new JPanel();
p1.setLayout(null);
l0 = new JLabel("欢迎使用添加员工信息界面");
l0.setFont(new Font("宋体", Font.PLAIN, 26));
l0.setBounds(100, 0, 350, 40); // 添加标签及文本框
for (int i = 0; i < bq.length; i++) {
if (i == 4 || i == 3) {
bq[i].setFont(new Font("宋体", Font.PLAIN, 26));
bq[i].setBounds(70, 80 + i * 70, 130, 26);
} else {
bq[i].setBounds(120, 80 + i * 70, 130, 28);
bq[i].setFont(new Font("宋体", Font.PLAIN, 26));
}
t[i].setBounds(220, 80 + i * 70, 130, 26);
t[i].setFont(new Font("宋体", Font.PLAIN, 26));
p1.add(t[i]);
p1.add(bq[i]);
// bq[i].addActionListener(this);
}
// 添加确定按钮
an1 = new JButton("确定");
an1.setBounds(100, 420, 100, 30);
an1.setFont(new Font("宋体", Font.PLAIN, 26));
an1.setForeground(Color.blue);
p1.add(an1);
an1.addActionListener(this);
// 添加返回按钮
an2 = new JButton("返回");
an2.setBounds(260, 420, 100, 30);
an2.setFont(new Font("宋体", Font.PLAIN, 26));
an2.setForeground(Color.blue);
p1.add(an2);
an2.addActionListener(this); // 提示信息 p1.add(l0);
this.add(p1);
this.setTitle("添加工作人员信息");
this.setBounds(100, 100, 500, 600);
this.setResizable(false);
this.setResizable(false);// 固定界面大小,不允许最大化
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","lulu1101");//修改数据库名,还有密码
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == an1) {
if (t[0].getText().isEmpty() || t[1].getText().isEmpty()
|| t[2].getText().isEmpty() || t[3].getText().isEmpty()
|| t[4].getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "文本框不能为空!!!", "消息对话框!",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
try {
String u0 = t[0].getText();
String u1 = t[1].getText();
String u2 = t[2].getText();
String u3 = t[3].getText();
String u4 = t[4].getText();
String sql = "insert into employee(number,name,salary,num,work)"
+ " values ('"
+ u0
+ "','"
+ u1
+ "','"
+ u2
+ "','" + u3 + "','" + u4 + "')";
sm = ct.prepareStatement(sql);
int a=sm.executeUpdate(sql);
if(a>0){
JOptionPane.showMessageDialog(this, "添加成功!",
"消息对话框!", JOptionPane.INFORMATION_MESSAGE);
for(int i=0;i<t.length;i++){
JTextField jf=t[i];
jf.setText("");
}
}else{
JOptionPane.showMessageDialog(this, "对不起!添加失败!",
"消息对话框!", JOptionPane.INFORMATION_MESSAGE);
}
sm.close();
} catch (SQLException ee) {
ee.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Baidu in = new Baidu();
}}