英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论