我用JBuilder做好登陆界面了,连接数据库应该这样写的:
package denglu;
import java.sql.*;
public class Login
{
public static void main(String[] args)
{
String sql;
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:students";
String user="sa";
String password="";
con= DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
sql="select * from user";
rs=stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我就是不明白怎么才能与"登陆"这个按钮连接起来
             请高手给指点一下  谢谢

解决方案 »

  1.   

    最简单的就是点击登陆提交给一个servlet在servlet里处理
      

  2.   

    不明白   这个要用servlet吗?
      

  3.   

    在登录按钮的单击事件按钮中调用这个函数。最好不要用main方法,改一个方法名。
      

  4.   

    用servlet和jsp都行.
    1.新建servlet,把main()方法里的代码拷到doPost()及doGet()方法中,
    username和password从request中取出.
    2.新建jsp,把main()方法里的代码写到<%%>中.
      

  5.   

    如果是B/S项目,用JSP+Servlet就可以,或是用Struts框架,定义一个<form action="servlet名称" method="post/get">……</form>,里面放入用户名和密码文本框,<input type="submit" value="登陆">提交输入的内容,servlet中写数据库方法,最好推荐使用地址池,可以使用JDBC或C3PO的,后者没有JDBC的Bug,推荐使用。
    如果是C/S项目,用事件和监听,给Button控件添加ActionListener和ActionEvent事件,具体的实现方法,你还是多看看书吧,书上都有!
      

  6.   

    谁能给讲明白点  数据库的连接到底是怎么样连接的,比如说我写好了
    package denglu; 
    import java.sql.*; 
    public class Login 

    public static void conn() 

    String sql; 
    Connection con; 
    Statement stmt; 
    ResultSet rs; 
    try 

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    String url="jdbc:odbc:students"; 
    String user="sa"; 
    String password=""; 
    con= DriverManager.getConnection(url,user,password); 
    stmt=con.createStatement(); 
    sql="select * from user"; 
    rs=stmt.executeQuery(sql); 
    while(rs.next()) 

    System.out.println(rs.getString(1)); 
    System.out.println(rs.getString(2)); 

    rs.close(); 
    stmt.close(); 
    con.close(); 

    catch(Exception e) 

    e.printStackTrace(); 



    我的登陆界面也画好了,
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    import java.awt.Font;
    import java.awt.Dimension;public class Frame1 extends JFrame {
        public Frame1() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            getContentPane().setLayout(null);        jButton1.setBounds(new Rectangle(46, 164, 83, 25));
            jButton1.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
            jButton1.setActionCommand("登陆");
            jButton1.setText("登陆");
            jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
            this.getContentPane().setBackground(SystemColor.control);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setForeground(UIManager.getColor(
                    "FormattedTextField.selectionBackground"));
            jTextField1.setBounds(new Rectangle(164, 66, 125, 21));
            jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
            jLabel1.setText("帐 号:");
            jLabel1.setBounds(new Rectangle(94, 62, 70, 28));
        jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
        jPasswordField1.setBounds(new Rectangle(164, 100, 126, 22));
        jPasswordField1.addActionListener(new Frame1_jPasswordField1_actionAdapter(this));
            jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
            jLabel2.setText("密 码:");
            jLabel2.setBounds(new Rectangle(94, 101, 72, 24));
            jButton3.setMaximumSize(new Dimension(57, 23));
            jButton3.setMinimumSize(new Dimension(57, 23));
            jButton2.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
            this.getContentPane().add(jTextField1);
            jButton3.setBounds(new Rectangle(257, 165, 87, 24));
            jButton3.setActionCommand("密码找回");
            jButton3.setText("密码找回");
            jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
            this.getContentPane().add(jPasswordField1);
            this.getContentPane().add(jLabel1);
            this.getContentPane().add(jLabel2);
            this.getContentPane().add(jButton1);
            this.getContentPane().add(jButton2);
            this.getContentPane().add(jButton3);
            jButton2.setBounds(new Rectangle(150, 164, 83, 25));
            jButton2.setText("取消");
        }    JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        JTextField jTextField1 = new JTextField();
      JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
      JPasswordField jPasswordField1 = new JPasswordField();
        JButton jButton3 = new JButton();
        public void jButton1_actionPerformed(ActionEvent e) {
                  }
                }我下面应该怎么写才能连接到数据库呢?
          
      

  7.   

    点击登入按钮时是执行jButton1_actionPerformed这个方法么?
    若是的话根据你前面写的:在这个方法里调用conn()方法,这样就连上数据库了。
    但是我觉得conn()需要修改一下,加两个参数进去,就是从登入页面的得到的用户名和密码,这样才能和数据库里的数据比较,看用户是否存在,从而决定要跳转的页面。  
      

  8.   

    是应该有个比较的   应该在jButton1_actionPerformed这个方法里 
                            也就是说我做了比较   然后在这个方法里调用conn()就行了吗?
      

  9.   

    先要链库,也就是先调用conn()。
    比较是和数据库里的数据比较