当前位置: 代码迷 >> Java相关 >> 怎么用按钮连接数据库并显示
  详细解决方案

怎么用按钮连接数据库并显示

热度:472   发布时间:2006-10-22 10:32:40.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());}
}
}

搜索更多相关的解决方案: 数据库  按钮  

----------------解决方案--------------------------------------------------------
斑竹 帮帮忙啊 救命啊
----------------解决方案--------------------------------------------------------
你写的不行吗,有什么毛病?
----------------解决方案--------------------------------------------------------
以下是引用hsjljh在2006-10-22 20:21:38的发言:
斑竹 帮帮忙啊 救命啊

数据库连接的示例到处都有啊,并且我这里写的程序,你那边并不一定能运行的
因为数据库不一样,并且用户名和密码也不一样
----------------解决方案--------------------------------------------------------
先在命令行中实现
然后再换成gui的不就可以了
----------------解决方案--------------------------------------------------------

楼上的能不能具体说一下怎么做啊 我是想用按钮出发一个事件 那个事件是连接数据库 并且开启另一个界面显示数据


----------------解决方案--------------------------------------------------------
先学一些简单的再说吧
----------------解决方案--------------------------------------------------------
你在事件处理器里new 一下你要跳转的那个类啊,就弹窗口出来了
----------------解决方案--------------------------------------------------------
只要把要注册的驱动啊 什么的连接函数的放在一个方法里 当你点按纽的时候调用着个函数不就好了

[此贴子已经被作者于2006-10-27 18:35:13编辑过]



----------------解决方案--------------------------------------------------------

呵呵 谢谢各位的指导 我知道该怎么做了 非常感谢


----------------解决方案--------------------------------------------------------
  相关解决方案