重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
把sql打印出来看看,运行一下你的sql,看能不能成功。
创新互联公司主要从事做网站、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务托里,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
把这个sql语句:insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')"; 直接在数据库里运行下看看能不能成功。
String username = "",password = "",passwordagain = ""; // 定义用户名和密码
将该变量等于为全局变量 或局部变量即可
界面建议用可视化来做,美观且便捷。下面这个是完全用代码写的,仅供参考。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Register extends JFrame {
JLabel jl1 = new JLabel("用户名");
JTextField jt1 = new JTextField();
JLabel jl2 = new JLabel("邮箱");
JTextField jt2 = new JTextField();
JLabel jl3 = new JLabel("密码");
JPasswordField jpw1 = new JPasswordField();
JLabel jl4 = new JLabel("密码确认");
JPasswordField jpw2 = new JPasswordField();
JButton register = new JButton("注册");
JButton clean = new JButton("清空");
public Register(){
setLayout(new GridLayout(5,2));
add(jl1);
add(jt1);
add(jl2);
add(jt2);
add(jl3);
add(jpw1);
add(jl4);
add(jpw2);
add(register);
add(clean);
String name = jt1.getText();
String email = jt2.getText();
String pw = jpw1.getText();
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");
Statement sta = con.createStatement();
sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)");
JOptionPane.showMessageDialog(null,"注册成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){
ex.getStackTrace();
}
}
});
clean.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt1.setText("");
jt2.setText("");
jpw1.setText("");
jpw2.setText("");
}
});
}
public static void main(String[] args){
Register frame = new Register();
frame.setTitle("用户注册");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
//这个是我写的,里面有连接数据库的部分。你可以拿去参考一下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用户名:");
JLabel lbl2 = new JLabel("密码:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("确定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
this.setTitle("登陆");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 数据库名为Car_zl,密码为1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名为user,user_ID和User_ps是存放用户名和密码的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陆成功!");
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Test {
private JFrame frame;
private JTextField jid,jname,jsex,jedu,jlike;
private JLabel lid,lname,lsex,ledu,llike;
private JButton ok;
private JButton quit;
public Test(){//用户ID、姓名、性别、学历、爱好
frame=new JFrame("用户注册");
jid=new JTextField(10);
jname=new JTextField(10);
jsex=new JTextField(10);
jedu=new JTextField(10);
jlike=new JTextField(10);
lid=new JLabel("用户id:");
lname=new JLabel("用户名:");
lsex=new JLabel("性别:");
ledu=new JLabel("学历:");
llike=new JLabel("爱好:");
ok=new JButton("注册");
quit=new JButton("取消");
init();
showMe();
addEventHandler();
}
private void addEventHandler() {
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public void init(){
frame.setLayout(new BorderLayout(2,2));
Border titled = BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.CYAN));
JPanel center = new JPanel();
center.setBorder(titled);
center.setLayout(new GridLayout(5,2,9,10));
JPanel south=new JPanel();
south.setLayout(new FlowLayout());
center.add(lid);
center.add(jid);
center.add(lname);
center.add(jname);
center.add(lsex);
center.add(jsex);
center.add(ledu);
center.add(jedu);
center.add(llike);
center.add(jlike);
south.add(ok);
south.add(quit);
frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
public void showMe(){
frame.setLocation(500, 300);
frame.setSize(210,280);
frame.setResizable(false);//不能拖动大小
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test().showMe();
}
}