数据库在regediter中userinfo_Data
谢谢了!
[此贴子已经被作者于2006-12-23 17:42:00编辑过]
[此贴子已经被作者于2006-12-23 17:42:00编辑过]
//添加数据
public boolean add(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("insert into userinfo values(?,?,?)");
ps.setString(1,getName());
ps.setString(2,getPassword());
ps.setString(3,getAddress());
rs=ps.executeQuery();
flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
//调用代码
public void btadd_actionPerformed(ActionEvent e) {
database db = new database();
db.setName(this.txtyhm.getText().trim());
db.setPassword(this.txtmm.getText().trim());
db.setAddress(this.txtaddress.getText().trim());
if (db.getName().equals("")) {
JOptionPane.showMessageDialog(this, "用户名为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.getPassword().equals("")) {
JOptionPane.showMessageDialog(this, "密码为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.getAddress().equals("")) {
JOptionPane.showMessageDialog(this, "地址为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.search()) {
JOptionPane.showMessageDialog(this, "数据库中已经存在该用户名", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
db.add();
JOptionPane.showMessageDialog(this, "恭喜,添加成功", "弹出信息",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
//查找信息
public boolean search(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("select * from userinfo where name=?");
ps.setString(1,getName());
rs=ps.executeQuery();
if(rs.next()){
flag = true;
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
}
没有,这样我把代码全贴出来你看一下!
package project2;
import java.io.*;
import java.sql.*;
import java.util.*;
//import javax.swing.JOptionPane;
//import java.lang.*;
class database implements Serializable {
public database() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//创建数据库对象
private Connection con;
private PreparedStatement ps;
private Statement s;
private ResultSet rs;
//定义属性
private String name;
private String password;
private String address;
//连接数据库
private boolean getConnection()
{
boolean flag=false;
try
{
//JDBC直连驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=userinfo";
con=DriverManager.getConnection(url,"sa","");
/*JDBC桥驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:userinfo";
con=DriverManager.getConnection(url,"sa","");*/
flag=true;
}
catch(Exception ex)
{
System.out.println("连接失败");
}
return flag;
}
//验证用户和密码
public boolean getselect()
{
boolean flag=false;
try
{
this.getConnection();
ps=con.prepareStatement("select * from userinfo where name=? and password=?");
ps.setString(1,this.getName());
ps.setString(2,this.getPassword());
//判断是否有数据存在
rs=ps.executeQuery();
if(rs.next())
{
flag=true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
this.closeData();
}
return flag;
}
//添加数据
public boolean add(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("insert into userinfo values(?,?,?)");
ps.setString(1,getName());
ps.setString(2,getPassword());
ps.setString(3,getAddress());
rs=ps.executeQuery();
flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
}
//删除信息
public boolean delete(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("delete from userinfo where name=?");
ps.setString(1,getName());
rs=ps.executeQuery();
flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
}
//修改信息
public boolean update(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("update userinfo set password=?,address=? where name=?");
ps.setString(1,getPassword());
ps.setString(2,getAddress());
ps.setString(3,getName());
rs=ps.executeQuery();
flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
}
//查找信息
public boolean search(){
boolean flag=false;
try{
this.getConnection();
ps=con.prepareStatement("select * from userinfo where name=?");
ps.setString(1,getName());
rs=ps.executeQuery();
if(rs.next()){
flag = true;
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return flag;
}
//显示所有信息
public Vector display(){
Vector vv=new Vector();
try{
this.getConnection();
ps=con.prepareStatement("select * from userinfo");
rs=ps.executeQuery();
while(rs.next()){
Vector v1=new Vector();
v1.add(rs.getString("name").trim());
v1.add(rs.getString("address").trim());
vv.add(v1);
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
this.closeData();
}
return vv;
}
public Vector select(){
Vector v4=new Vector();
try{
this.getConnection();
ps=con.prepareStatement("select * from userinfo where name=?");
ps.setString(1,getName());
rs=ps.executeQuery();
if(rs.next()){
v4.add(rs.getString("name").trim());
v4.add(rs.getString("password").trim());
v4.add(rs.getString("address").trim());
}
}catch(Exception ex){
ex.printStackTrace();
}
finally {
this.closeData();
}
return v4;
}
//关闭数据库
private void closeData(){
try {
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
if(con!=null)
con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String getAddress(){
return address;
}
public String setName(String name){
this.name=name;
return name;
}
public String setPassword(String password){
this.password=password;
return password;
}
public String setAddress(String address){
this.address=address;
return address;
}
private void jbInit() throws Exception {
}
}
package project2;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.TitledBorder;
import java.lang.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class userinfo
extends JDialog {
JPanel panel1 = new JPanel();
TitledBorder titledBorder1 = new TitledBorder("");
JPanel jPanel1 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
GridLayout gridLayout1 = new GridLayout();
TitledBorder titledBorder2 = new TitledBorder("");
//private JTable jtb;
JTable jtb = new JTable();
JLabel jLabel1 = new JLabel();
JTextField txtyhm = new JTextField();
JLabel jLabel2 = new JLabel();
JPasswordField txtmm = new JPasswordField();
JLabel jLabel3 = new JLabel();
JTextField txtaddress = new JTextField();
JButton btadd = new JButton();
JButton btdisp = new JButton();
JButton btupdate = new JButton();
JButton btserch = new JButton();
JButton btdel = new JButton();
public userinfo(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
public userinfo() {
this(new Frame(), "userinfo", false);
panel1.setSize(600, 450);
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setLayout(gridLayout1);
panel1.setSize(600, 450);
gridLayout1.setColumns(0);
gridLayout1.setRows(2);
this.getContentPane().setLayout(null);
panel1.setBounds(new Rectangle(0, 0, 600, 450));
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
jLabel1.setText("用户名:");
jLabel1.setBounds(new Rectangle(2, 19, 71, 28));
txtyhm.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
txtyhm.setText("");
txtyhm.setBounds(new Rectangle(82, 15, 88, 30));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
jLabel2.setText("密码:");
jLabel2.setBounds(new Rectangle(174, 19, 50, 28));
txtmm.setText("");
txtmm.setBounds(new Rectangle(211, 15, 88, 30));
jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
jLabel3.setText("地址:");
jLabel3.setBounds(new Rectangle(2, 54, 54, 28));
txtaddress.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
txtaddress.setText("");
txtaddress.setBounds(new Rectangle(60, 54, 238, 31));
btadd.setBounds(new Rectangle(7, 109, 63, 26));
btadd.setText("添加");
btadd.addActionListener(new userinfo_btadd_actionAdapter(this));
btdisp.setBounds(new Rectangle(152, 109, 63, 26));
btdisp.setText("显示");
btdisp.addActionListener(new userinfo_btdisp_actionAdapter(this));
btupdate.setBounds(new Rectangle(232, 109, 63, 26));
btupdate.setText("修改");
btupdate.addActionListener(new userinfo_btupdate_actionAdapter(this));
btserch.setBounds(new Rectangle(119, 165, 63, 26));
btserch.setActionCommand("查找");
btserch.setText("查找");
btserch.addActionListener(new userinfo_btserch_actionAdapter(this));
btdel.setBounds(new Rectangle(78, 109, 63, 26));
btdel.setText("删除");
btdel.addActionListener(new userinfo_btdel_actionAdapter(this));
panel1.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jtb);
panel1.add(jPanel1, null);
jPanel1.add(jLabel1);
jPanel1.add(txtyhm);
jPanel1.add(jLabel2);
jPanel1.add(txtmm);
jPanel1.add(jLabel3);
jPanel1.add(txtaddress);
jPanel1.add(btadd);
jPanel1.add(btdisp);
jPanel1.add(btupdate);
jPanel1.add(btserch);
jPanel1.add(btdel);
this.getContentPane().add(panel1, null);
jPanel1.setBorder(titledBorder2);
// DefaultTableModel model=new DefaultTableModel(cells,header);
//jtb.setModel(model);
}
public void btadd_actionPerformed(ActionEvent e) {
database db = new database();
db.setName(this.txtyhm.getText().trim());
db.setPassword(this.txtmm.getText().trim());
db.setAddress(this.txtaddress.getText().trim());
if (db.getName().equals("")) {
JOptionPane.showMessageDialog(this, "用户名为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.getPassword().equals("")) {
JOptionPane.showMessageDialog(this, "密码为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.getAddress().equals("")) {
JOptionPane.showMessageDialog(this, "地址为空", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
if (db.search()) {
JOptionPane.showMessageDialog(this, "数据库中已经存在该用户名", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
else {
db.add();
JOptionPane.showMessageDialog(this, "恭喜,添加成功", "弹出信息",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
public void btdisp_actionPerformed(ActionEvent e) {
database db = new database();
Vector alluserinfo = db.display();
Vector cel = new Vector();
cel.add("用户名");
cel.add("地址信息");
DefaultTableModel model = new DefaultTableModel(alluserinfo, cel);
jtb.setModel(model);
Vector vvv=db.select();
txtmm.setText(vvv.get(1).toString());
txtaddress.setText(vvv.get(2).toString());
}
public void btdel_actionPerformed(ActionEvent e) {
database db = new database();
db.setName(this.txtyhm.getText().trim());
if (db.search()) {
JOptionPane.showMessageDialog(this, "是否真的要删除本用户", "警告信息",
JOptionPane.WARNING_MESSAGE);
db.delete();
JOptionPane.showMessageDialog(this, "该用户已经被删除", "信息",
JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(this, "该用户不存在", "警告信息",
JOptionPane.ERROR_MESSAGE);
}
}
public void btupdate_actionPerformed(ActionEvent e) {
database db = new database();
db.setPassword(this.txtmm.getText().trim());
db.setAddress(this.txtaddress.getText().trim());
db.setName(this.txtyhm.getText().trim());
if (db.search()) {
JOptionPane.showMessageDialog(this, "是否真的要修改本用户", "警告信息",
JOptionPane.WARNING_MESSAGE);
db.update();
}
else {
JOptionPane.showMessageDialog(this, "数据库中没有本用户", "错误信息",
JOptionPane.ERROR_MESSAGE);
}
Vector vvv=db.select();
txtmm.setText(vvv.get(1).toString());
txtaddress.setText(vvv.get(2).toString());
}
public void btserch_actionPerformed(ActionEvent e) {
database db=new database();
db.setName(txtyhm.getText().trim());
if(db.search())
JOptionPane.showMessageDialog(this, "找到用户", "信息",
JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(this, "没有找到用户", "错误信息",
JOptionPane.ERROR_MESSAGE);
Vector vvv=db.select();
txtmm.setText(vvv.get(1).toString());
txtaddress.setText(vvv.get(2).toString());
}
}
class userinfo_btserch_actionAdapter
implements ActionListener {
private userinfo adaptee;
userinfo_btserch_actionAdapter(userinfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btserch_actionPerformed(e);
}
}
class userinfo_btupdate_actionAdapter
implements ActionListener {
private userinfo adaptee;
userinfo_btupdate_actionAdapter(userinfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btupdate_actionPerformed(e);
}
}
class userinfo_btdel_actionAdapter
implements ActionListener {
private userinfo adaptee;
userinfo_btdel_actionAdapter(userinfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btdel_actionPerformed(e);
}
}
class userinfo_btdisp_actionAdapter
implements ActionListener {
private userinfo adaptee;
userinfo_btdisp_actionAdapter(userinfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btdisp_actionPerformed(e);
}
}
class userinfo_btadd_actionAdapter
implements ActionListener {
private userinfo adaptee;
userinfo_btadd_actionAdapter(userinfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btadd_actionPerformed(e);
}
}