简体   繁体   English

在 JDBC 上使用键盘插入数据时出错

[英]Error in insert data using keyboard on JDBC

In JDBC, I want to insert data using input from the keyboard.在 JDBC 中,我想使用键盘输入插入数据。

As a result of finding the method, I came to know the scanner and wrote the code as follows.由于找到方法,我开始了解扫描仪并编写代码如下。

package DB;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

class insert{

    Connection con;

    Statement stmt;

    Scanner scan = new Scanner(Syetem.in);

    public insert() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            con = DriverManager.getConnection(url,"scott","1234");
            stmt = con.createStatement();
        } catch(ClassNotFoundException ce) {
            System.out.println(ce.getMessage());
        } catch(SQLException se){
            System.out.println(se.getMessage());
        }
    }

    public void disconnect() throws SQLException{
        if (stmt!=null) stmt.close();
        if (con!=null) con.close();
    }

    public void insert() throws SQLException{
        System.out.println("name:")
        String employee_name=scan.next();

        System.out.println("domain:")
        String street=scan.next();

        System.out.println("country:")
        String city=scan.next();

        String sql="insert into information values('"+name+"', '"+domain+"', '"+country+"')";

        int n=stmt.executeUpdate(sql);
    }

}

But it didn't run and got an error... The default method cannot be found in the class.但是没有运行,报错了……class中找不到默认方法。 Define a default method in the following format.按以下格式定义默认方法。 public static void main (String [] args)公共 static 无效主(字符串 [] 参数)

Where should I put the main function to fix the error?我应该把主要的 function 放在哪里来修复错误?

What is the problem?问题是什么? The name of the table to be inserted is 'information'.要插入的表的名称是“信息”。

Any help would be greatly appreciated任何帮助将不胜感激

  • Because JDBC is not yet familiar, if possible, if you write a full query including a connection as in the above query, I would be very grateful.因为 JDBC 还不熟悉,如果可能的话,如果你写一个完整的查询,包括上面查询中的连接,我将非常感激。

*my oracle version is 11g *我的oracle版本是11g

For your code to run you need to provide a method that works as an entry point for your program, that method has the signature public static void main(String[] args) besides that, your class should be named Insert with capital I first because thats de standard y second because you have a method called insert, you will need to have something like this:为了让您的代码运行,您需要提供一个方法作为程序的入口点,该方法具有签名public static void main(String[] args)除此之外,您的 class 应该首先命名为 Insert 并带有大写 I,因为那是标准的 y 秒,因为您有一个名为 insert 的方法,您将需要这样的东西:

public class Insert {
  public Insert() { 
  ...    
  }

  public void disconnect() throws SQLException {
  ...
  }

  public void insert() throws SQLException {
  ...
  }

  public static void main(String[] args) {
    new Insert().insert();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM