用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

Java 数据库链接

2015-04-18 作者: java源代码大全举报

[java]代码库

package student;

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

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StuDataBase implements ActionListener {
	Connection con;
	Statement state;

	// 连接数据库的账号和密码
	String conAccount;
	String conPassword;
	JTextField textAccount;
	JPasswordField textPass;

	JFrame frame;
	JButton sure1;

	JDialog dialog;
	JButton sure2;

	MainFrame mainF;

	public StuDataBase(MainFrame mainF) {
		frame = new JFrame("登录");
		conAccount = "student";
		conPassword = "123456";
		this.mainF = mainF;

		frame.setSize(305, 165);
		frame.setLocation(550, 250);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		buildConnect();
		buildpane();
		frame.setVisible(true);

	}

	public void buildConnect() {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void buildpane() {
		// frame的面板
		JPanel fPane = (JPanel) frame.getContentPane();
		fPane.setLayout(new GridLayout(3, 2));
		Font f = new Font("MyFont", Font.PLAIN, 15);
		JLabel account = new JLabel("账号", JLabel.CENTER);
		JLabel password = new JLabel("密码", JLabel.CENTER);
		account.setFont(f);
		password.setFont(f);

		textAccount = new JTextField();
		textPass = new JPasswordField();
		sure1 = new JButton("确定");

		sure1.addActionListener(this);

		fPane.add(account);
		fPane.add(textAccount);
		fPane.add(password);
		fPane.add(textPass);
		fPane.add(new JLabel());
		fPane.add(sure1);

		// dialog的面板
		dialog = new JDialog(frame);
		dialog.setSize(277, 137);
		dialog.setLocation(565, 265);
		JPanel dPane = (JPanel) dialog.getContentPane();

		dPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		JLabel tip = new JLabel("账号密码错误,请重新输入", JLabel.CENTER);
		tip.setFont(f);

		sure2 = new JButton("确定");
		sure2.addActionListener(this);

		c.gridwidth = 3;
		dPane.add(tip, c);

		c.insets = new Insets(10, 10, 0, 0);
		c.gridy = 1;
		c.gridx = 2;
		c.gridwidth = 1;
		dPane.add(sure2, c);

	}

	public void createTable() {
		try {

			state.execute("Create table major(ID Integer Primary key, Name String);");
			state.execute("Create table student(ID Integer Primary key,Name String,Sex String,Class Integer, Major Integer, Constraint fk foreign key(Major) references major(ID));");
		} catch (SQLException e) {
			System.out.println("表已存在");
		}

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == sure1) {
			if (textAccount.getText().equals(conAccount)
					&& String.valueOf(textPass.getPassword()).equals(
							conPassword))
				try {
					con = DriverManager.getConnection("JDBC:ODBC:Student",
							conAccount, conPassword);
					state = con.createStatement();
					createTable();
					frame.dispose();
					mainF.state = state;
					mainF.isStuExist(0);
					mainF.isMajorExist(0);
					mainF.setVisible(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			else {
				dialog.setVisible(true);
			}
		} else {
			dialog.setVisible(false);
		}
	}

}
//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...