java(jdbc)向oracle中插入记录
表A字段a,boracle数据库地址:192.168.161.3SID:NMS求高手教教小弟,如何用Jdbc往数据库中插入记录,查询记录我会,小弟在线等!!!...
表A
字段 a,b
oracle数据库地址:192.168.161.3
SID:NMS
求高手教教小弟,如何用Jdbc往数据库中插入记录,查询记录我会,小弟在线等!!! 展开
字段 a,b
oracle数据库地址:192.168.161.3
SID:NMS
求高手教教小弟,如何用Jdbc往数据库中插入记录,查询记录我会,小弟在线等!!! 展开
6个回答
展开全部
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();//关闭数据库操作(自己写)
}
}
}
希望对你有帮助,不过代码这东西的写法很多。关键看自己的理解
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();//关闭数据库操作(自己写)
}
}
}
希望对你有帮助,不过代码这东西的写法很多。关键看自己的理解
展开全部
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();//关闭数据库操作(自己写)
}
}
}
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();//关闭数据库操作(自己写)
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
兄弟查询都会了插入就这样写呗
INSERT INTO A VALUES(a_value,b_value)
INSERT INTO A VALUES(a_value,b_value)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
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);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
PreparedStatement ps = conn.prepareStatement("insert into 表A(a,b) values(?,?)");
ps.setString(0,"aaaa");
ps.setString(1,"bbbb");
ps.execute();
conn.close();
ps.setString(0,"aaaa");
ps.setString(1,"bbbb");
ps.execute();
conn.close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询