我用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();
}
}
}
我就是不明白怎么才能与"登陆"这个按钮连接起来
请高手给指点一下 谢谢
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,把main()方法里的代码拷到doPost()及doGet()方法中,
username和password从request中取出.
2.新建jsp,把main()方法里的代码写到<%%>中.
如果是C/S项目,用事件和监听,给Button控件添加ActionListener和ActionEvent事件,具体的实现方法,你还是多看看书吧,书上都有!
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) {
}
}我下面应该怎么写才能连接到数据库呢?
若是的话根据你前面写的:在这个方法里调用conn()方法,这样就连上数据库了。
但是我觉得conn()需要修改一下,加两个参数进去,就是从登入页面的得到的用户名和密码,这样才能和数据库里的数据比较,看用户是否存在,从而决定要跳转的页面。
也就是说我做了比较 然后在这个方法里调用conn()就行了吗?
比较是和数据库里的数据比较