| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2554 人关注过本帖
标题:保存数据到数据库成功
只看楼主 加入收藏
李文森it
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-3-5
结帖率:0
收藏
 问题点数:0 回复次数:4 
保存数据到数据库成功
package com.demo;


import javax.swing.AbstractButton;
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框

import java.awt.Font;//字体
import java.awt.Color;//颜色

import javax.swing.JPasswordField;//密码框

import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;//消息窗口

import org.omg.CORBA.Request;



public class Login extends JFrame{
private static final long serialVersionUID = 1L;
public JPanel pnluser;
 public JLabel lbluserLogIn;
 public JLabel lbluserName;
 public JLabel lbluserPWD;
 public JTextField txtName;
 public JPasswordField pwdPwd;
 public JButton btnSub;
 public JButton btnReset;
 public JButton zhuce;


 public Login() {
  pnluser = new JPanel();
  lbluserLogIn = new JLabel();
  lbluserName = new JLabel();
  lbluserPWD = new JLabel();
  txtName = new JTextField();
  pwdPwd = new JPasswordField();
  btnSub = new JButton();
  btnReset = new JButton();
  zhuce = new JButton();
  userInit();
 }
 
 public void userInit(){
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
  this.setBounds(500, 300, 300, 200);
  this.setSize(300,200);//设置框架大小为长300,宽200
  this.setResizable(false);//设置框架不可以改变大小
  this.setTitle("用户登录");//设置框架标题
  this.pnluser.setLayout(null);//设置面板布局管理
  this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
  this.lbluserLogIn.setText("用户登录");//设置标签标题
  this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
  this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
  this.lbluserName.setText("用户名:");
  this.lbluserPWD.setText("密    码:");
  this.btnSub.setText("登录");
  this.btnReset.setText("重置");
  this.zhuce.setText("注册");
  this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20
  this.lbluserName.setBounds(50,55,60,20);
  this.lbluserPWD.setBounds(50,85,60,25);
  this.txtName.setBounds(110,55,120,20);//用户名输入方框
  this.pwdPwd.setBounds(110,85,120,20);//密码输入方框
  this.btnSub.setBounds(60,120,60,20);
  this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
   {
    public void actionPerformed(ActionEvent e){
     btnsub_ActionEvent(e);
    }   
   }
  );
  this.btnReset.setBounds(130,120,60,20);

  this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
   {
    public void actionPerformed(ActionEvent e){
     btnreset_ActionEvent(e);
    }   
   }
  );  
  
  this.zhuce.setBounds(210,120,60,20);
  this.zhuce.addActionListener(new ActionListener()//匿名类实现ActionListener接口
  { public void actionPerformed(ActionEvent e){
      new Zhuce();
   }   
  }
 );  
  this.pnluser.add(lbluserLogIn);//加载标签到面板
  this.pnluser.add(lbluserName);
  this.pnluser.add(lbluserPWD);
  this.pnluser.add(txtName);
  this.pnluser.add(pwdPwd);
  this.pnluser.add(btnSub);
  this.pnluser.add(btnReset);
  this.pnluser.add(zhuce);
  this.add(pnluser);//加载面板到框架
  this.setVisible(true);//设置框架可显  
 }
 
 public void btnsub_ActionEvent(ActionEvent e){
 
     Connection dbConn = null;
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
        // 连接服务器和数据库ServletUser
        String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Northwind"; //数据库的名
        String user = "sa"; // 默认用户名
        String userpassword = "123456"; // 密码
        
String name=txtName.getText();//输入的用户名
        
        String mima=pwdPwd.getText();//输入的密码

        //String sqlStr="select CustomerID, CompanyName, ContactName,userName,userPwd from Customers";   //调用数据库的表Customers
        
        if(name.equals("")||mima.equals(""))//如果没输用户名或密码,则提示对不起,请输入用户名或密码
        {
            JOptionPane.showInputDialog("对不起,请输入用户名或密码");
        }
        else{
        try  {   //这里的异常处理语句是必需的.否则不能通过编译!   
            Class.forName(driverName);//注意这里,网上的和这里不同要改为这样
               
            Connection con = DriverManager.getConnection(dbURL, user, userpassword);  //数据库的登录名和密码,sa,123456
            System.out.println("创建连接对像成功!");           
            Statement st = con.createStatement();   
            
           // ResultSet rs = st.executeQuery("select * from login where namer='"+name+"'");
         
            //String sql=st.executeQuery("select * from login where namer='"+name+"'");
            
             ResultSet rs = st.executeQuery("select * from login where namer='"+name+"'");
            
            Statement stmt=con.createStatement();
            //rs=stmt.executeQuery(sql);
          // ResultSet rs=stmt.executeQuery("insert into login(namer,pwd) values("+"‘"+name+"‘"+","+mima+"‘"+")");
            //stmt.execute(rs);
            
            //rs=stmt.executeQuery(sql);
            
            
             String    sql="insert into login(namer,pwd) values("+"'"+name+"'"+","+"'"+mima+"'"+")";
                    stmt.execute(sql);
            
            
            
            System.out.println("添加成功");//userName是输入的用户名,txtName,password1是输入的密码
                       //用户(用户名,密码) 是 表名(列名,列名);
                  
            
            
             /*  if(rs.next()){
                    
                    
                    
                     
                     System.out.println(name);  
                     System.out.println(mima);  
                     System.out.println(rs.getString("pwd"));  

                     if(rs.getString("pwd").equals(mima))//数据库的类型要设置为varchar(50);
                      {
                          JOptionPane.showInputDialog( "系统登录成功,恭喜你!");
                          setVisible(false);
                          new chenggongjiemian();
                          
                          
                      }
                  
                      else
                      {
                          JOptionPane.showInputDialog("对不起,密码错误,请重新输入,登陆失败");
                      }
                 
                }*/
            rs.close();      
            st.close();      
            con.close();   
            }
        catch(Exception err) {      
            err.printStackTrace(System.out);        }
        
        
    finally{
            //进行资源的释放
            if(dbConn!=null){
                try {
                    dbConn.close();
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }}}
   
 }
 
 private String txtName() {
    // TODO Auto-generated method stub
    return null;
}

private Object rs(String string) {
    // TODO Auto-generated method stub
    return null;
}

private void setUserPwd(int int1) {
    // TODO Auto-generated method stub
   
}

private void setUserName(int int1) {
    // TODO Auto-generated method stub
   
}

public void btnreset_ActionEvent(ActionEvent e){
  txtName.setText("");
  pwdPwd.setText("");
 }
 
 public static void main(String[] args){
  new Login();
 
  
 }
}
搜索更多相关主题的帖子: package import 数据库 文本框 标签 
2015-03-29 16:03
李文森it
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-3-5
收藏
得分:0 
是用java编写的
2015-03-29 16:05
北辰风
Rank: 1
等 级:新手上路
帖 子:19
专家分:4
注 册:2013-1-14
收藏
得分:0 
2015-03-29 17:56
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
推荐试试https://,可以和世界各地的大牛一起修改代码,感觉很爽的
2015-03-31 19:27
gdlyhp
Rank: 1
等 级:新手上路
威 望:1
帖 子:3
专家分:0
注 册:2015-4-1
收藏
得分:0 
public class JdbcText {

    public static void main(String[] args) {
        Connection dbConn = null;
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
        // 连接服务器和数据库ServletUser
        String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=ServletUser";
        String userName = "sa"; // 默认用户名
        String userPwd = "123456"; // 密码
        String sqlStr="select CustomerID, CompanyName, ContactName from Customers";
        try {
            Class.forName(driverName);
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("Connection Successful!"); // 如果连接成功
            System.out.println(userName);
            
            
            System.out.println("类实例化成功!");   
            Connection con = DriverManager.getConnection(dbURL, userName, userPwd);   
            System.out.println("创建连接对像成功!");           
            Statement st = con.createStatement();   
            System.out.println("创建Statement成功!");     
            ResultSet rs = st.executeQuery(sqlStr);   
            System.out.println("操作数据表成功!");      
            System.out.println("----------------!");   
            
            while(rs.next())             {         
                System.out.print(rs.getString("CustomerID") + www. "    ");      
                System.out.print(rs.getString("CompanyName") + "    ");   
                System.out.println(rs.getString("ContactName"));      
                }           
            rs.close();      
            st.close();      
con.close();  

            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //进行资源的释放
            if(dbConn!=null){
                try {
                    dbConn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
2015-04-01 10:00
快速回复:保存数据到数据库成功
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016087 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved