刘日怪! 2022-06-26 23:29 采纳率: 66.7%
浏览 72
已结题

线程“main”中的异常java.sql.SQLException:没有为jdbc://127.0.0.1:3306/rja 2101找到合适的驱动程序

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcTest007 {
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection("jdbc:mysql//127.0.0.1:3306/rja2101 ?serverTimezone=UTC","root","123456");
System.out.println(conn);
PreparedStatement pstmt =(PreparedStatement) conn.prepareStatement("select * from/zs");
ResultSet resultSet = pstmt.executeQuery();
while (resultSet.next()) {
String id =resultSet.getString("xh");
String sname =resultSet.getString("xm");
String ssex =resultSet.getString("sex");
String sbirth =resultSet.getNString("birth");
String sdep =resultSet.getString("dep");
System.out.println(id+""+sname+""+ssex+""+sbirth+""+sdep);

        }
        resultSet.close();
        pstmt.close();
        conn.close();
    

    
}

}

另外一段为import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Add extends JFrame {

JLabel jlxh=new JLabel("学号");
JLabel jlxm=new JLabel("姓名");
JLabel jlsex=new JLabel("性别");
JLabel jlbirth=new JLabel("出生日期");
JLabel jldep=new JLabel("院系");


JTextField jtxh=new JTextField("",20);
JTextField jtxm=new JTextField("",20);
JTextField jtsex=new JTextField("",20);
JTextField jtbirth=new JTextField("",20);
JTextField jtdep=new JTextField("",20);

JButton buttonadd=new JButton("添加");
JButton buttonreturn=new JButton("返回");
//创建构造窗体的空参方法
public Add() {
JPanel jpxh=new JPanel();
JPanel jpxm=new JPanel();
JPanel jpsex=new JPanel();
JPanel jpbirth=new JPanel();
JPanel jpdep=new JPanel();
JPanel jpforbutton=new JPanel(new GridLayout(1,1));

jpxh.add(jlxh);
jpxh.add(jtxh);

jpxm.add(jlxm);
jpxm.add(jtxm);

jpsex.add(jlsex);
jpsex.add(jtsex);

jpbirth.add(jlbirth);
jpbirth.add(jtbirth);
//院系
jpdep.add(jldep);
jpdep.add(jtdep);
//按钮
jpforbutton.add(buttonadd);
jpforbutton.add(buttonreturn);
//将面板放入窗体
this.add(jpxh);
this.add(jpxm);
this.add(jpsex);
this.add(jpbirth);
this.add(jpdep);
this.add(jpforbutton);
//设置窗体信息
this.setTitle("添加学生信息");
this.setLayout(new GridLayout(7,1));
this.setLocation(400,300);
this.setSize(350, 300);
this.setVisible(true);

buttonadd.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        //得到连接
        Connection conn=null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql//127.0.0.1:3306/rja2101 ?serverTimezone=UTC","root","123456");
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        //像表中插入数据
        String sql="INSERT INTO xs(学号,姓名,性别,出生日期,所属院系) values(?,?,?,?)" ;
        
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        
        try {
            ps.setString(1,jtxh.getText());
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        try {
            ps.setString(2,jtxm.getText());
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        try {
            ps.setString(3,jtsex.getText());
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        try {
            ps.setString(4,jtbirth.getText());
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        try {
            ps.setString(5,jtdep.getText());
        } catch (SQLException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        
        try {
            ps.executeUpdate();
        } catch (SQLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        System.out.println("成功增加!!!");
        
      

              }
});
//为返回按钮注册监听
buttonreturn.addActionListener(new ActionListener() {
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        dispose();
    }
});

}

}

运行报错结为Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc://127.0.0.1:3306/rja2101
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at cn.cswu002.JdbcTest007.main(JdbcTest007.java:11)

  • 写回答

2条回答 默认 最新

  • haikuokuangtu 2022-06-27 00:29
    关注

    改成这样(数据库的名称不能写错)
    jdbc:mysql://localhost:3306/rja2101
    或者
    jdbc:mysql://localhost:3306/rja2101?serverTimezone=UTC
    中间不要有空格

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月8日
  • 已采纳回答 6月30日
  • 创建了问题 6月26日

悬赏问题

  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码