| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 516 人关注过本帖
标题:[紧急求助]怎么用按钮连接数据库并显示
只看楼主 加入收藏
hsjljh
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2005-10-26
收藏
 问题点数:0 回复次数:0 
[紧急求助]怎么用按钮连接数据库并显示

怎么用按钮连接数据库并显示这是我写的代码,我想输入了数据库的用户名&密码后,点击按钮可以连接数据库,并显示出来,请各位大哥帮帮忙啊,小弟急啊!!!!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

class namepassDB extends JFrame{

static windowsevent we=new windowsevent();
static JLabel l1=new JLabel("username:",JLabel.LEFT);
static JLabel l2=new JLabel("password:",JLabel.LEFT);
static JButton jb=new JButton("OK");

void bulidConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,int wx,int wy){
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridheight=gh;
gbc.gridwidth=gw;
gbc.weightx=wx;
gbc.weighty=wy;
}

public namepassDB(){
super("username and password");
setSize(290,110);
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints Constraints=new GridBagConstraints();
JPanel pane=new JPanel();
pane.setLayout(gridbag);

bulidConstraints(Constraints,0,0,1,1,10,40);
Constraints.fill=GridBagConstraints.NONE;
Constraints.anchor=GridBagConstraints.EAST;
gridbag.setConstraints(l1,Constraints);
pane.add(l1);

bulidConstraints(Constraints,1,0,1,1,90,0);
Constraints.fill=GridBagConstraints.HORIZONTAL;
JTextField jtf=new JTextField();
gridbag.setConstraints(jtf,Constraints);
pane.add(jtf);

bulidConstraints(Constraints,0,1,1,1,0,40);
Constraints.fill=GridBagConstraints.NONE;
Constraints.anchor=GridBagConstraints.EAST;
gridbag.setConstraints(l2,Constraints);
pane.add(l2);

bulidConstraints(Constraints,1,1,1,1,0,0);
Constraints.fill=GridBagConstraints.HORIZONTAL;
JPasswordField jpf=new JPasswordField();
jpf.setEchoChar('*');
gridbag.setConstraints(jpf,Constraints);
pane.add(jpf);

bulidConstraints(Constraints,0,2,2,1,0,20);
Constraints.fill=GridBagConstraints.NONE;
Constraints.anchor=GridBagConstraints.CENTER;
gridbag.setConstraints(jb,Constraints);
pane.add(jb);

setContentPane(pane);
}

public static void main(String args[]){
namepassDB f=new namepassDB();
f.addWindowListener(we);
f.show();
try{connect();}
catch(Exception e4){System.out.println(e4.toString());}
}

static class windowsevent extends WindowAdapter{

public void windowclosing(WindowEvent e){
System.exit(0);
}
}

public void mouseClicked(MouseEvent e1){
try{connect();}
catch(Exception e5){System.out.println(e5.toString());}
}

public static void connect() throws SQLException,ClassNotFoundException{
Connection conn;
Statement stmt;
ResultSet rs;
String username=l1.getText();
String password=l2.getText();

try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e2)
{System.out.println(e2.toString());}

try{conn=DriverManager.getConnection("jdbc:odbc:student",username,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from students");
while(rs.next())
{
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.print(rs.getString(4)+" ");
System.out.println(rs.getString(5));
System.out.println();
}
}
catch(SQLException e3){System.out.println(e3.toString());}
}
}

搜索更多相关主题的帖子: 连接数据库 password 用户名 import 大哥 
2006-10-23 21:51
快速回复:[紧急求助]怎么用按钮连接数据库并显示
数据加载中...
 
   



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

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