十五779 2022-05-13 18:26 采纳率: 100%
浏览 288
已结题

提问,java代码连接数据库时出错

java获取数据库数据失败

问题相关代码,请勿粘贴截图

package edu.dali17_2;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import edu.dali17_2.GetDBConnection;

public class Data extends JFrame{
JPanel jp1,jp2;
JLabel jl1;
JButton jb1;
JTextField jtf1,jtf2;
JTextArea jta;

Data(){
    init();
    this.setLocation(580, 270);
    this.setSize(300, 300);
    this.setTitle("数据库查询");
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setVisible(true);
}

void init() {
    //this.setLayout(new FlowLayout());
    
    jp1 =new JPanel();
    jl1 = new JLabel("请输入学号:");
    jtf1 = new JTextField(10);
    jb1 = new JButton("确定");
    jb1.addActionListener(new ActionListener() {
        
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO 自动生成的方法存根
            Connection con;
            Statement sql = null;
            ResultSet res;
            
            con = GetDBConnection.connectDB("stu", "root", "123456");
            if(con==null) {
                System.out.println("连接失败");
                return ;
            }
            String strsql,str;
            str = jtf1.getText().trim();        //trim删除字符串左右的空格;
            if(str.equals("")) {
                strsql = "select * from student";
                System.out.println("查询失败");
            }
            else {
                strsql = "select * from student where id="+str;    //处理字符型
            }
            
            try {
                sql = con.createStatement();
                res = sql.executeQuery(strsql);
                jta.setText("");
                
                while(res.next()) {
                    
                    int id = res.getInt(1);
                    String name = res.getString(2);
                    String sex = res.getString(3);
                    Date date = res.getDate(4);
                    
                    jta.append(String.valueOf(id)+"  "+name+"  "+sex+"  "+String.valueOf(date)+"\n");
                    //jta.append(res.getString(1)+"  "+res.getString(2)+"  "+res.getString(3)+"  "+res.getString(4)+"\n");
                    
                    
                }
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
            
            
        }
    });
    jta = new JTextArea(6,20);
    
    jp1.add(jl1);
    jp1.add(jtf1);
    jp1.add(jb1);
    jp1.add(jta);
    this.add(jp1);
}

}

package edu.dali17_2;

import java.sql.*;
public class GetDBConnection {
public static Connection connectDB(String DBName,String id,String p) {
Connection con = null;
String uri =
"jdbc:mysql://localhost:3306/"+DBName+"?useSSL=true&characterENcoding=utf-8";
try { Class.forName("com.sql.jdbc.DRiver");

    }
    catch(Exception e) {}
    try {
        con = DriverManager.getConnection(uri,id,p);
    }
    catch(SQLException e) {}
    return con;
}

}

package edu.dali17_2;

public class DataTest {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    Data DB = new Data();
}

}

运行结果及报错内容

连接失败
查询失败
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "con" is null
at edu.dali17_2.Data$1.actionPerformed(Data.java:57)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我想要达到的结果

究竟是哪里出错了,来个会的人,指导指导。

  • 写回答

2条回答 默认 最新

  • Tomshidi 2022-05-13 18:53
    关注

    GetDBConnection代码中异常捕获的地方改一下,改成下面这样,再运行,把报错发出来

    try { 
              Class.forName("com.sql.jdbc.DRiver");
    }catch(Exception e) {
        e.printStackTrace()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月22日
  • 已采纳回答 5月14日
  • 创建了问题 5月13日

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路