您的位置:首页 > 编程语言 > Java开发

java制作登陆界面验证问题mysql为数据库

2013-01-15 22:18 537 查看
http://www.oschina.net/question/875147_81038

java制作登陆界面验证问题mysql为数据库

参加OSC应用开发大赛,拿Nexus 4手机
活动详情

就是登陆界面点击登陆验证用户名和密码然后进入下一个界面的代码仅仅需要一个按钮的就好= =!数据库已经测试连接成功 附数据库和两个界面的代码



这个是连接代码

view source

print?

01
import
java.sql.Connection;
02
import
java.sql.DriverManager;
03
import
java.sql.SQLException;
04
public
class
ConnectionDemo02{
05
//public ConnectionDemo02 conn;
06
public

static
final
String DBDRIVER=
"org.gjt.mm.mysql.Driver"
;
07
public

static
final
String DBURL=
"jdbc:mysql://localhost:3306/wxf"
;
08
public

static
final
String DBUSER=
"root"
;
09
public

static
final
String DBPASS=
"26533621"
;
10
public

static
void
main(String[] args){
11
Connection conn=
null
;
12
try
{
13
Class.forName(DBDRIVER);
14
}
catch

(ClassNotFoundException e){
15
e.printStackTrace();
16
}
17
try
{
18
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
19
}
catch
(SQLException e){
20
e.printStackTrace();
21
}
22
System.out.print(conn);
23
try
{
24
conn.close();
25
}
catch
(SQLException e){
26
e.printStackTrace();
27
}
28
}
29
}
这是登陆界面

view source

print?

01
import
java.awt.*;
02
import
java.awt.event.*;
03
import
java.sql.*;
04
import
javax.swing.*;
05
import
java.awt.geom.*;
06
import
java.util.Vector;
07
public
class
denglu
extends

JFrame
08
{
09
public

Label name=
new
Label(
"用户名"
);
10
public

Label pass=
new
Label(
"密码"
);
11
public

TextField txtname=
new

TextField();
12
public

TextField txtpass=
new

TextField();
13
public

Button btok=
new

Button(
"登陆"
);
14
public

Button btexit=
new

Button(
"取消"
);
15
public

denglu()
16
{
17
setTitle(
"欢迎使用工资管理系统"
);
18
setLayout(
null
);
19
20
setResizable(
false
);
21
setSize(
500
,
350
);
22
23
Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
24
Dimension frm=
this
.getSize();
25
setLocation((scr.width-frm.width)/
2
,(scr.height-frm.height)/
2
-
18
);
26
txtpass.setEchoChar(
'*'
);
27
name.setBounds(
70
,
260
,
40
,
27
);
28
pass.setBounds(
70
,
300
,
40
,
27
);
29
txtname.setBounds(
120
,
260
,
120
,
27
);
30
txtpass.setBounds(
120
,
300
,
120
,
27
);
31
btok.setBounds(
340
,
260
,
100
,
28
);
32
btexit.setBounds(
340
,
300
,
100
,
28
);
33
add(name);
34
add(txtname);
35
add(pass);
36
add(txtpass);
37
add(btok);
38
add(btexit);
39
setVisible(
true
);
40
}
41
public

static
void
main(String args[])
42
{
43
dengludl=
new

denglu();
44
}
45
}
这个是点击后进入的界面代码

view source

print?

01
import
java.awt.*;
02
import
java.awt.event.*;
03
import
java.awt.geom.*;
04
import
javax.swing.*;
05
import
javax.swing.event.*;
06
import
java.sql.*;
07
public
class
yonghu
08
{
09
public

static
void
main(String args[])
10
{
11
JFrame jf=
new

JFrame();
12
jf.setTitle(
"aaa"
);
13
jf.setBounds(
300
,
250
,
300
,
200
);
14
jf.setVisible(
true
);
15
}
16
}
标签:Eclipse

MySQL
我想问同样的问题 共0个人想要问同样的问题
补充话题说明»

分享到

收藏

0

举报

踩0 |
顶0

按评价排序 |
显示最新答案 |

回页面顶部共有4个答案
我要回答»





大人 回答于 2012-11-28 10:13

举报

view source

print?

01
import
java.sql.Connection;
02
import
java.sql.DriverManager;
03
import
java.sql.SQLException;
04
public
class
ConnectionDemo02{
05
//public ConnectionDemo02 conn;
06
public

static
final
String DBDRIVER=
"org.gjt.mm.mysql.Driver"
;
07
public

static
final
String DBURL=
"jdbc:mysql://localhost:3306/wxf"
;
08
public

static
final
String DBUSER=
"root"
;
09
public

static
final
String DBPASS=
"26533621"
;
10
public

Connection getConn(){
11
Connection conn=
null
;
12
try
{
13
Class.forName(DBDRIVER);
14
}
catch

(ClassNotFoundException e){
15
e.printStackTrace();
16
}
17
try
{
18
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
19
}
catch
(SQLException e){
20
e.printStackTrace();
21
}
22
23
return

conn;
24
}
25
}
view source

print?

01
<pre
class
=
"brush:java; toolbar: true; auto-links: false;"
>
import

java.sql.Connection;
02
import
java.sql.PreparedStatement;
03
import
java.sql.ResultSet;
04
import
java.sql.SQLException;
05
import
java.text.SimpleDateFormat;
06
import
java.util.Calendar;
07
import
java.util.Date;
08
09
public
class
Login {
10
PreparedStatement ps =
null
;
11
ResultSet rs =
null
;
12
Connection conn =
null
;
13
14
public

boolean
verify(String name,String password ) {
15
boolean

result=
false
;
16
17
String sql =
"select * from usekey where idcard=? and password=?"
;
18
Connection con =
new
ConnectionDemo02().getConn();
19
try

{
20
ps = con.prepareStatement(sql);
21
ps.setString(
1
,name);
22
ps.setString(
2
,password);
23
rs = ps.executeQuery();
24
25
if

(rs.next()) {
//验证成功
26
result=
true
;
27
}
28
29
}

catch
(SQLException e) {
30
// TODO Auto-generated catch block
31
e.printStackTrace();
32
}

finally
{
33
try

{
34
if

(rs !=
null
)
35
rs.close();
36
if

(ps !=
null
)
37
ps.close();
38
if

(conn !=
null
)
39
conn.close();
40
}

catch
(SQLException e) {
41
// TODO Auto-generated catch block
42
e.printStackTrace();
43
}
44
}
45
46
return

result;
47
}
48
49
}
50
</pre>
51
<br>
---共有 3 条评论 ---



非常感谢感谢
再感谢 已经成功了 有点小问题已经完成 谢谢(1个月前 by wangms)回复



回复
@wangms : 写在一个里面也没问题,不过面向对象讲究类的重复利用,最好按业务功能的不同写到不同的类中(1个月前 by 大人)回复




这个验证是不是要新用一个 不能再原来里面加是吧0.0(1个月前 by wangms)回复

有帮助(0) |
没帮助(0) |
评论(3) |
引用此答案





wangms 回答于 2012-11-28 09:26

举报

高手们 没人知道么 帮帮小弟啊 卡了快1周了

有帮助(0) |
没帮助(0) |
评论(0) |
引用此答案





王振威 回答于 2012-11-28 09:43

举报

首先我明白为什么有三个main方法,一个java程序main方法是一个入口,应该只有一个,对于这里来讲main方法就应该是new出你的登录界面dengludl=new

denglu();了,另外你没有给登录这个button注册事件,他当然什么都不做了,大致的流程是这样,main中new出登录窗口,登录按钮注册点击事件,然后在注册的方法中连接数据库检测帐号和密码是否与用户输入的匹配,如果正确就展示用户界面。

---共有 1 条评论 ---



这个我知道
我就是写了监听了 一直错 然后一气之下都删掉了 然后看看能不能帮我写一段对的 只是确定的监听验证账号密码代码就好(1个月前 by wangms)回复

有帮助(0) |
没帮助(0) |
评论(1) |
引用此答案





大人 回答于 2012-11-28 09:51

举报

view source

print?

01
import
java.awt.*;
02
import
java.awt.event.*;
03
import
java.sql.*;
04
import
javax.swing.*;
05
import
java.awt.geom.*;
06
import
java.util.Vector;
07
08
public
class
denglu
extends

JFrame {
09
public

Label name =
new

Label(
"用户名"
);
10
public

Label pass =
new

Label(
"密码"
);
11
public

TextField txtname =
new

TextField();
12
public

TextField txtpass =
new

TextField();
13
public

Button btok =
new

Button(
"登陆"
);
14
public

Button btexit =
new

Button(
"取消"
);
15
16
public

denglu() {
17
setTitle(
"欢迎使用工资管理系统"
);
18
setLayout(
null
);
19
20
setResizable(
false
);
21
setSize(
500
,
350
);
22
23
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
24
Dimension frm =
this
.getSize();
25
setLocation((scr.width -frm.width) /
2
,
26
(scr.height -frm.height) /
2
-
18
);
27
txtpass.setEchoChar(
'*'
);
28
name.setBounds(
70
,
260
,
40
,
27
);
29
pass.setBounds(
70
,
300
,
40
,
27
);
30
txtname.setBounds(
120
,
260
,
120
,
27
);
31
txtpass.setBounds(
120
,
300
,
120
,
27
);
32
btok.setBounds(
340
,
260
,
100
,
28
);
33
btexit.setBounds(
340
,
300
,
100
,
28
);
34
add(name);
35
add(txtname);
36
add(pass);
37
add(txtpass);
38
add(btok);
39
add(btexit);
40
setVisible(
true
);
41
btok.addActionListener(
new

ActionListener() {
42
@Override
43
public

void
actionPerformed(ActionEvent arg0) {
44
if

(验证用户名和密码通过) {
45
denglu.
this
.dispose();
46
new

yonghu().init();
47
}
48
}
49
});
50
}
51
52
public

static
void
main(String args[]) {
53
dengludl =
new
denglu();
54
}
55
}
view source

print?

01
<pre
class
=
"brush:java; toolbar: true; auto-links: false;"
>
import

java.awt.*;
02
import
java.awt.event.*;
03
import
java.awt.geom.*;
04
import
javax.swing.*;
05
import
javax.swing.event.*;
06
import
java.sql.*;
07
08
public
class
yonghu {
09
public

void
init() {
10
JFrame jf =
new
JFrame();
11
jf.setTitle(
"aaa"
);
12
jf.setBounds(
300
,
250
,
300
,
200
);
13
jf.setVisible(
true
);
14
}
15
}</pre>
16
<br>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐