在使用JDBC键盘插入数据时出现错误。

huangapple go评论91阅读模式
英文:

Error in insert data using keyboard on JDBC

问题

在JDBC中我想要使用从键盘输入的数据来插入数据

为了找到方法我了解到了Scanner并且编写了以下代码

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(System.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);
}

}

但是它没有运行并且出现了错误...
在类中找不到默认方法。请以以下格式定义一个默认方法。public static void main (String [] args)

在哪里放置main函数以修复这个错误?

问题是什么?要插入的表的名称是'information'。

非常感谢任何帮助。

* 因为我还不太熟悉JDBC,如果可能的话,如果您编写一个包括连接的完整查询,就像上面的查询一样,我会非常感激。

* 我的Oracle版本是11g
英文:

In JDBC, I want to insert data using input from the keyboard.

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. Define a default method in the following format. public static void main (String [] args)

Where should I put the main function to fix the error?

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.

*my oracle version is 11g

答案1

得分: 0

以下是您的代码的翻译部分:

为了使您的代码运行您需要提供一个作为程序入口的方法该方法的签名为`public static void main(String[] args)`,除此之外您的类名应为Insert首字母大写因为这是标准其次您有一个名为insert的方法您需要有类似这样的内容

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

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

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

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

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 class Insert {
  public Insert() { 
  ...    
  }

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

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

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

</details>



huangapple
  • 本文由 发表于 2020年5月31日 02:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/62106999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定