-1
package test;

import java.sql.*;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        try{
            Scanner sc = new Scanner(System.in);
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "root");
            Statement stmt = (Statement) con.createStatement();
            System.out.print("Enter name to search");
            String name = sc.next();
            String query = "SELECT * FROM Contacts WHERE first_name = '" + name + "' ;";
            ResultSet rs = stmt.executeQuery(query);

            while(rs.next()){
                System.out.println("Last_Name: " + rs.getString("last_name"));
                System.out.println("First_Name: " + rs.getString("first_name"));
                System.out.println("Email: " + rs.getString("email"));
                System.out.println("Phone: " + rs.getString("mobile_phone"));

            }
        } catch(Exception e){
            System.out.println("ERROR: " + e.
     }

我将 jdbc.jar 导入到项目库中,但仍然出现错误:

Could not find or load main class test.Test
4

1 回答 1

0

你在使用一些IDE吗?你的类在“test”包中,所以如果你想直接从命令行执行你的程序,你应该进入 test 的父文件夹,然后启动:

java test.Test

于 2013-04-20T15:39:54.847 回答