保存数据到数据库成功
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();
}
}