如何在 SQL 开发人员使用 JDBC 的 table Oracle 中插入值
How to insert a value in a table Oracle in SQL Developer using JDBC
我想在 Oracle SQL Developer 中的 table 中插入一行。我有一组字符串,String name
、String address
和 String contact
。
如果我使用此代码将不起作用:
Connection conn = null;
Statement stmt = null;
String URL = "jdbc:oracle:thin:mariel@//localhost:1521/XEXDB";
String USER = "mariel";
String PASS = "1234";
String name = "a", address = "a", contact="a";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection(URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO CUSTOMER" +
"VALUES(CustNumSeq.NEXTVAL, name, address, contact)";
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
Logger.getLogger(FinishTransaction.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
finally{
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
修改您的代码如下
String sql = "INSERT INTO CUSTOMER " +
"VALUES(CustNumSeq.NEXTVAL, name, address, contact)"
你必须在 table name(CUSTOMER) 和 VALUES[=13= 之间保留一个 space ]
根据修改 post
String sql = "INSERT INTO CUSTOMER " +
"VALUES('"+ name+"','"+ address+"','" +contact+"')"
注意:您应该始终更喜欢使用 PreparedStatement 而不是 Statement 作为
PreparedStatement 比 Statement.Please 有更多优势,请参阅 link 了解更多信息
我想在 Oracle SQL Developer 中的 table 中插入一行。我有一组字符串,String name
、String address
和 String contact
。
如果我使用此代码将不起作用:
Connection conn = null;
Statement stmt = null;
String URL = "jdbc:oracle:thin:mariel@//localhost:1521/XEXDB";
String USER = "mariel";
String PASS = "1234";
String name = "a", address = "a", contact="a";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection(URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO CUSTOMER" +
"VALUES(CustNumSeq.NEXTVAL, name, address, contact)";
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
Logger.getLogger(FinishTransaction.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
finally{
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
修改您的代码如下
String sql = "INSERT INTO CUSTOMER " +
"VALUES(CustNumSeq.NEXTVAL, name, address, contact)"
你必须在 table name(CUSTOMER) 和 VALUES[=13= 之间保留一个 space ]
根据修改 post
String sql = "INSERT INTO CUSTOMER " +
"VALUES('"+ name+"','"+ address+"','" +contact+"')"
注意:您应该始终更喜欢使用 PreparedStatement 而不是 Statement 作为 PreparedStatement 比 Statement.Please 有更多优势,请参阅 link 了解更多信息