把sql打印出來看看,運(yùn)行一下你的sql,看能不能成功。
創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)托里,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
把這個sql語句:insert into student(id,name,age,major,college)values('"+id+"','"+name+"','"+age+"','"+major+"','"+college+"')"; 直接在數(shù)據(jù)庫里運(yùn)行下看看能不能成功。
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("密碼確認(rèn)");
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);
}
}
//這個是我寫的,里面有連接數(shù)據(jù)庫的部分。你可以拿去參考一下
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數(shù)據(jù)庫
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 數(shù)據(jù)庫名為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、姓名、性別、學(xué)歷、愛好
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("學(xué)歷:");
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();
}
}