JAVA连接SQLSERVER2008实现教程

先实现简单的登录界面,这里用了两个类,一个UI(主界面)类,一个连接数据库方法类

之间需要添加驱动和环境变量,具体的准备工作请看前人的工作

UI类

package ui;

import java.awt.event.*;
import javax.swing.*;
import conn.connect;
import ui.MainFrame;
//导入相应的包
//import java.awt.event.ActionListener;
//import java.awt.event.ActionEvent;
//import javax.swing.JButton;
//import javax.swing.JFrame;
//import javax.swing.JLabel;
//import javax.swing.JPanel;
//import javax.swing.JPasswordField;
//import javax.swing.JTextField;

public class ui extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2373066478342237427L;

	JPanel jp = new JPanel();

	JLabel[] jlArray = { new JLabel("用户名"), new JLabel("口 令"), new JLabel("") };
	JButton[] jbArray = { new JButton("登陆"), new JButton("退出") };
	JTextField jtxtName = new JTextField();
	JPasswordField jtxtPassword = new JPasswordField();

	public ui() { // 构造方法

		jp.setLayout(null);

		for (int i = 0; i < 2; i++) {
			jlArray[i].setBounds(30, 20 + i * 50, 80, 26);
			jbArray[i].setBounds(50 + i * 110, 130, 80, 26);
			jp.add(jlArray[i]);
			jp.add(jbArray[i]);
			jbArray[i].addActionListener(this);
		}
		jtxtName.setBounds(80, 20, 180, 30);
		jp.add(jtxtName);
		jtxtName.addActionListener(this);
		jtxtPassword.setBounds(80, 70, 180, 30);
		jp.add(jtxtPassword);
		jtxtPassword.setEchoChar('*');
		jtxtPassword.addActionListener(this);
		jlArray[2].setBounds(10, 180, 300, 30);
		jp.add(jlArray[2]);
		this.add(jp);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("登陆");
		this.setResizable(false);
		this.setBounds(800, 370, 300, 250);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) { // 事件监听方法
		boolean flag;
		String username = jtxtName.getText();
		String password = String.valueOf(jtxtPassword.getPassword());
		flag = connect.validator(username, password);
		if (e.getSource() == jtxtName) {
			jtxtPassword.requestFocus();
		} else if (e.getSource() == jbArray[1]) {
			System.exit(0);}
		else {
			if (flag) {
				setVisible(false);
				new MainFrame(username).setVisible(true);
			} else
				jlArray[2].setText("登陆错误");	
		}
		
	}

	

	public static void main(String[] args) {// main方法
		new ui();
		
	}
}

数据库连接类

package conn;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class connect {
	public static boolean validator(String username, String password) {
		Connection conn;
		Statement stmt;
		ResultSet rs;
		boolean flag = false;
		String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student;";
		username=TransactSQLInjection(username);
		 password=TransactSQLInjection(password);
		String sql = "select * FROM dbo.usercode where U_id='" + username + "'and Password='" + password + "'";
		try {
			// 连接数据库
			conn = DriverManager.getConnection(url, "sa", "root");
			// conn = DriverManager.getConnection(url, "user8", "u0008");// 建立Statement对象
			stmt = conn.createStatement();
			/**
			 * Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到数据库。
			 */
			// 执行数据库查询语句
			rs = stmt.executeQuery(sql);
			/**
			 * ResultSet executeQuery(String sql) throws SQLException 执行给定的 SQL 语句,该语句返回单个
			 * ResultSet 对象
			 */
			while (rs.next()) {
				if (rs != null) {
					flag = true;
					break;
				}

			}
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("数据库连接失败");
		}
		return flag;
	}
	 public static String TransactSQLInjection(String str){
		return str.replaceAll("([';])+|(--)+","");//防止数据库注入的代码
	}

	public static void main(String[] args) {

	}
}
(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐