MySQL连接不起作用我收到错误

时间:2018-05-12 12:00:02

标签: mysql jdbc

所以我想检索表格的一行,但首先我想让它工作,然后我会做其余的工作,所以下面是代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import java.util.*;
import java.sql.*;


public class ExampleForDB {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        try{  
            Class.forName("com.mysql.jdbc.Driver");  
            Connection con=DriverManager.getConnection(  
            "jdbc:mysql://localhost:3306/******","*****","******"); 

                System.out.print("Successful connection with MySQL!");
            //here sonoo is database name, root is username and password  
            Statement stmt=con.createStatement();  
            ResultSet rs=stmt.executeQuery("select Number1 from lottery1955-00 where id=1");  
            System.out.println(rs.getInt(1)+"  "+rs.getString(1));  
            con.close();  
        }catch (SQLException e ) {
              e.printStackTrace();
          } 
            }  


    }

在这里,您可以看到我的错误,我尝试了很多不同的方式,我在StackOverflow中找到了这些方法,但它们都没有为我工作,你能分享一下你的经验吗? 看到下面的错误

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at ExampleForDB.main(ExampleForDB.java:16)

0 个答案:

没有答案