在使用eclipse时的错误

上周着重学习JDBC的相关内容,在练习的过程中,eclipse出现不能运行的情况,其发出的错误是:The selection cannot be launched, and there are no recent launches.

 

 代码如下:

package 数据库;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

@SuppressWarnings("unused")
public class 测试 {
    public boolean login(String username ,String pass) throws SQLException, ClassNotFoundException {
        //准备四大参数
        String driverClassName="com.mysql.jdbc.Driver";
        String url ="jdbc:mysql://localhost:3306/1123?useSSL=false";
        String mysqlUsername ="root";
        String mysqlPassword="123456";
        //加载驱动类
        Class.forName(driverClassName);
        //得到cannection
        java.sql.Connection con =  DriverManager.getConnection(url,mysqlUsername,mysqlPassword);
        //得到statement
        java.sql.Statement stmt =  con.createStatement();
        //给出sql语句,调用,stmt的executequery(),的到resultset
        String mysql ="select * from t_user where username ='" + username + " ' and '" + pass + "'";
        ResultSet rs = stmt.executeQuery(mysql);
        
        return rs.next();
    }
    public static void main(String[] args) throws Exception{
        boolean bool =login ("root","123456");
        System.out.println(bool);
                
    }
}

问题的原因是没有配置main函数

在名为数据库的package上右键,选择run as,然后选Run Configuration,找到Java Application,设定好 main class就可以运行啦。

 

posted @ 2020-04-26 11:17  源星  阅读(493)  评论(0)    收藏  举报