| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2040 人关注过本帖
标题:java中数据库查询,搭配简单的图形界面进行查询
只看楼主 加入收藏
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
结帖率:88.89%
收藏
 问题点数:0 回复次数:2 
java中数据库查询,搭配简单的图形界面进行查询
在第一个文本框中输入学号后点查询,然后再第二个文本框中显示此学生的性别

报错:  
Exception in thread "main" java.lang.Error: 无法解析的编译问题:
    访问限制:由于对必需的库 C:\Program Files (x86)\Java\jdk1.8.0_60\jre\lib\rt.jar 具有一定限制,因此无法访问类型 AbstractButton 中的方法 addActionListener(ActionListener)

    at dblx.Sjk1_5.main(Sjk1_5.java:31)

源代码:
        package dblx;
import java.sql.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Sjk1_5 {
    private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String url="jdbc:sqlserver://neverAgain\\neverAgain:1433;DatabaseName=Myschool";
    private static String user="sa";
    private static String password="111111";
   
    static JFrame frm=new JFrame("数据库查询");
    static JTextField jtf1=new JTextField();
    static JTextField jtf2=new JTextField();
    static JButton bt=new JButton("查询");
    static JLabel jlab=new JLable("学号");
    static JLabel jlab1=new JLable("性别");
    static String xh=null;
    static String jg=null;
   
    public static void main(String[] args) {
        
        frm.add(jlab);
        frm.add(jtf1);
        frm.add(jlab1);
        frm.add(jtf2);
        frm.add(bt);
        frm.setSize(400, 200);
        bt.addActionListener(new JT());
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String selectsql="select Sex from Student where Sno=?";
        try{
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,password);
            ps=conn.prepareStatement(selectsql);
            ps.setString(1,xh);
            rs=ps.executeQuery();
            jg=rs.getString("Sex");
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                if(rs!=null)rs.close();
                if(ps!=null)ps.close();
                if(conn!=null)conn.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        }
   

    }
    static class JT implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            xh=jtf1.getText();
            jtf2.setText(jg);
        }
    }

}
搜索更多相关主题的帖子: 数据库查询 package thread 源代码 import 
2017-04-09 23:49
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
收藏
得分:0 
好了,
感悟:resultset结果集,只有调用next方法才能让指针指向结果集的第一行,要不然总是为null,就会报错:结果集没有当前行!

package dblx;
import java.sql.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Sjk1_5 {
    private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String url="jdbc:sqlserver://neverAgain\\neverAgain:1433;DatabaseName=Myschool";
    private static String user="sa";
    private static String password="111111";
   
    static JFrame frm=new JFrame("数据库查询");
    static JTextField jtf1=new JTextField();
    static JTextField jtf2=new JTextField();
    static JButton bt=new JButton();
    static JLabel jlab=new JLabel();
    static JLabel jlab1=new JLabel();
   
   
    public static void main(String[] args) {
        
       jlab.setText("学号");
       jlab1.setText("性别");
       bt.setText("查询");
        frm.add(jlab);
        frm.add(jtf1);
        frm.add(jlab1);
        frm.add(jtf2);
        frm.add(bt);
        GridLayout grid=new GridLayout(3,2);
        frm.setLayout(grid);
        jtf1.setBackground(Color.RED);
        frm.setSize(300,220);
        bt.setBounds(20, 300, 50, 50);
        bt.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                 String xh=null;
                String jg=null;
                Connection conn=null;
                PreparedStatement ps=null;
                ResultSet rs=null;
                String selectsql="select * from Student where Sno=?";
                xh=jtf1.getText();
                System.out.println(xh);
               
                try{
                    
                    Class.forName(driver);
                    conn=DriverManager.getConnection(url,user,password);
                    ps=conn.prepareStatement(selectsql);
                    ps.setString(1,xh);
                    rs=ps.executeQuery();
                    while(rs.next()){
                        jg=rs.getString("Sex");
                    }
                    jtf2.setText(jg);
                    System.out.println(jg);
                    
                }
                catch(Exception e1){
                    e1.printStackTrace();
                }
                finally{
                    
                    
                    
                    
                    try{
                        if(rs!=null)rs.close();
                        if(ps!=null)ps.close();
                        if(conn!=null)conn.close();
                    }
                    catch(Exception e1)
                    {
                        e1.printStackTrace();
                    }
                    
                }
            }
            
        }
                );
        
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setVisible(true);

    }
   

}

唯望君安~
2017-04-11 09:10
快速回复:java中数据库查询,搭配简单的图形界面进行查询
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035512 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved