java(jdbc)向oracle中插入记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java(jdbc)向oracle中插入记录相关的知识,希望对你有一定的参考价值。

表A
字段 a,b
oracle数据库地址:192.168.161.3
SID:NMS
求高手教教小弟,如何用Jdbc往数据库中插入记录,查询记录我会,小弟在线等!!!

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
* 此类专门连接数据库
*/

public class Jdbc
private Connection conn;
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String USER = "scott";
private static final String PASSWORD = "123456";

public Jdbc()
try
//加载驱动
Class.forName(DRIVER);

//连接数据库
this.conn = DriverManager.getConnection(URL,USER,PASSWORD);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();




//返回Connection
public Connection getConnection()
return this.conn;



--------------------------------------------
一下是插入数据的方法调用
public class Operate
private Connection conn;
private PreparedStatement prst;
private String sql;
public Operate()
Jdbc jdbc = new Jdbc();
this.conn = jdbc.getConnection();

public void insert()
sql = "insert into A values(a_value,b_value) ";
try
this.prst = this.conn.prepareStatement(sql);
this.prst.executeUpdate();
catch(Exception ex)
ex.printStackTrace();
finally
this.close();//关闭数据库操作(自己写)





希望对你有帮助,不过代码这东西的写法很多。关键看自己的理解
参考技术A import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
* 此类专门连接数据库
*/

public class Jdbc
private Connection conn;
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String USER = "scott";
private static final String PASSWORD = "123456";

public Jdbc()
try
//加载驱动
Class.forName(DRIVER);

//连接数据库
this.conn = DriverManager.getConnection(URL,USER,PASSWORD);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();




//返回Connection
public Connection getConnection()
return this.conn;



--------------------------------------------
一下是插入数据的方法调用
public class Operate
private Connection conn;
private PreparedStatement prst;
private String sql;
public Operate()
Jdbc jdbc = new Jdbc();
this.conn = jdbc.getConnection();

public void insert()
sql = "insert into A values(a_value,b_value) ";
try
this.prst = this.conn.prepareStatement(sql);
this.prst.executeUpdate();
catch(Exception ex)
ex.printStackTrace();
finally
this.close();//关闭数据库操作(自己写)



参考技术B 兄弟查询都会了插入就这样写呗
INSERT INTO A VALUES(a_value,b_value)
参考技术C Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.161.3 :1521:NMS", "用户名", "密码");
stm=conn.createStatement();
String sql="delete from user where userId='"+id+"'";
stm.executeUpdate(sql);本回答被提问者采纳
参考技术D PreparedStatement ps = conn.prepareStatement("insert into 表A(a,b) values(?,?)");
ps.setString(0,"aaaa");
ps.setString(1,"bbbb");
ps.execute();
conn.close();

以上是关于java(jdbc)向oracle中插入记录的主要内容,如果未能解决你的问题,请参考以下文章

JDBC 批量插入异常处理以了解特定的失败记录

从java程序中向oracle中插入数据,为啥插入顺序总是不对

如何从 Oracle 中的 JDBC 批量插入中获取生成的密钥?

JDBC插入数据实例

java - 如何使用rmi在java中的mysql(jdbc)中插入一条记录? [关闭]

如何得到JDBC Insert 语句执行后插入Oracle 数据库记录的主键