Swing+数据库连接
来源:互联网 发布:思途旅游网站源码 编辑:程序博客网 时间:2024/06/14 17:07
数据库类型:SQL Server2000
切记,不要忘了加驱动包!
DB.java
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DB {
- private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=UserInfo";
- //数据库路径:服务器所在的计算机为localhost:1433;
- //数据库名称:UserInfo
- private static final String UserName = "xsy"; //用户名称
- private static final String PassWord = "xsy123"; //用户密码
- private static Connection conn = null;
- public static Connection getConnection(){
- try {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动程序
- conn = DriverManager.getConnection(URL, UserName, PassWord); //创建数据库连接
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
test1.java
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.TitledBorder;
- public class test1 extends JFrame implements ActionListener{
- public static void main(String[] args) {
- test1 t1 = new test1();
- t1.setVisible(true); //设置窗体可见
- }
- JButton button1;
- JButton button2;
- JTextField textField1;
- JTextField textField2;
- JTextField textField3;
- public test1(){
- super();
- this.setTitle("查询基本信息");
- this.setBounds(200,200,350,260); //setSize(int width,int hight);setBounds(x,y,width,hight);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container p = getContentPane();
- p.setLayout(null);
- JLabel label1 = new JLabel("姓名:");
- label1.setBounds(80,10,40,20);
- p.add(label1);
- textField1 = new JTextField();
- textField1.setBounds(130,8,120,20);
- p.add(textField1);
- button1 = new JButton("查询");
- button1.setBounds(100,40,60,20);
- button1.addActionListener(this); //添加鼠标响应事件
- p.add(button1);
- button2 = new JButton("重置");
- button2.setBounds(180,40,60,20);
- button2.addActionListener(this);
- p.add(button2);
- JPanel resultPanel = new JPanel();
- resultPanel.setBounds(40,90,260,100);
- //定义指定颜色,指定标题的边框
- TitledBorder tb = new TitledBorder(BorderFactory.createLineBorder(new Color(255,0,0)),"查询结果");
- tb.setTitleColor(Color.blue);//设置标题颜色
- resultPanel.setBorder(tb); //将标题边框添加到面板中
- //resultPanel.setBackground(Color.yellow);
- resultPanel.setLayout(null);
- JLabel label2 = new JLabel("姓名:");
- label2.setBounds(80,110,40,20);
- p.add(label2);
- textField2 = new JTextField();
- textField2.setBounds(130,108,120,20);
- p.add(textField2);
- JLabel label3 = new JLabel("密码");
- label3.setBounds(80,140,40,20);
- p.add(label3);
- textField3 = new JTextField();
- textField3.setBounds(130,140,120,20);
- p.add(textField3);
- p.add(resultPanel);
- }
- public void actionPerformed(ActionEvent e){
- JButton jb = (JButton)e.getSource();
- if(jb == button1){
- String name = textField1.getText();
- String password = search(name);
- textField2.setText(name);
- textField3.setText(password);
- }
- if(jb == button2){
- textField1.setText("");
- }
- }
- public String search(String name){
- String password="";
- try {
- Connection conn = DB.getConnection();
- Statement stmt = conn.createStatement();
- String sql = "select UserPwd from User_info where UserName='"+name+"'";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- password = rs.getString(1);
- }
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return password;
- }
- }
- Swing+数据库连接
- Swing
- Swing..........
- swing
- swing
- Swing
- swing
- Swing
- swing
- swing
- Swing
- Swing
- Swing
- swing
- swing
- Swing
- Swing
- Swing
- 传智播客android系统开发全程笔记
- servlet的应用 ServletTest1 例子
- 【linux】——helloworld模块编写与测试
- MIS第一章学习认知
- oracle 九九乘法表的应用pl/sql块的实例(笨小孩)
- Swing+数据库连接
- 大家围观围观
- 我的c++
- 多表查询优化1
- 原来幸福可以这么简单
- 20110502
- 论坛规划
- c语言:/*题目:输入某年某月某日,判断这一天是这一年的第几天?*/
- 全球玩具贸易查询作业