java如何从oracle中读取的全部结果集里面动态的读取字段?
并不是在程序中预先设置好字段然后读取,而是把执行sql语句得到的结果集全部显示出来.代码如下但是执行起来只能读出一条数据中的第一列就报错了.谢谢大家!jbutton2.a...
并不是在程序中预先设置好字段然后读取,而是把执行sql语句得到的结果集全部显示出来.代码如下但是执行起来只能读出一条数据中的第一列就报错了.
谢谢大家!
jbutton2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
try{
//判断连接是否存在或者连接是否关闭
if(conn!=null&&!conn.isClosed()){
//创建Statement对象
Statement stmt=conn.createStatement();
//获取sql语句 eg:select * from test
String sql=null;
sql=jtextfield3.getText();
System.out.println("执行SQL语句:"+sql);
//文本域中累加
jtextarea.setText(jtextarea.getText()+"执行SQL语句:"+sql+"\n");
//返回数据库中符合条件记录
ResultSet rs=stmt.executeQuery(sql);
//显示数据库中符合条件记录
while(rs.next()){
while(rs.getBoolean(i)){
String str=rs.getString(i) ;//str默认为索引初始值1
jtextarea.setText(jtextarea.getText()+id+"\n");
i++;
}
rs.close();
stmt.close();
str=1;
}
else{
jtextarea.setText(jtextarea.getText()+"数据库已关闭或连接不存在!\n");
}
}
catch(SQLException se){
jtextarea.setText(jtextarea.getText()+"SQL执行失败!\n");
se.printStackTrace();
}
}
});
} 展开
谢谢大家!
jbutton2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
try{
//判断连接是否存在或者连接是否关闭
if(conn!=null&&!conn.isClosed()){
//创建Statement对象
Statement stmt=conn.createStatement();
//获取sql语句 eg:select * from test
String sql=null;
sql=jtextfield3.getText();
System.out.println("执行SQL语句:"+sql);
//文本域中累加
jtextarea.setText(jtextarea.getText()+"执行SQL语句:"+sql+"\n");
//返回数据库中符合条件记录
ResultSet rs=stmt.executeQuery(sql);
//显示数据库中符合条件记录
while(rs.next()){
while(rs.getBoolean(i)){
String str=rs.getString(i) ;//str默认为索引初始值1
jtextarea.setText(jtextarea.getText()+id+"\n");
i++;
}
rs.close();
stmt.close();
str=1;
}
else{
jtextarea.setText(jtextarea.getText()+"数据库已关闭或连接不存在!\n");
}
}
catch(SQLException se){
jtextarea.setText(jtextarea.getText()+"SQL执行失败!\n");
se.printStackTrace();
}
}
});
} 展开
1个回答
展开全部
你的意思是向程序传入SQL语句然后根据传入的SQL语句进行查询?
如果这样,我建议是这样的,写一个单饿独的类来进行数据库CRUD
例如:
package com.assets.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static final String user = "assets";
private static final String password = "assets";
private static final String url = "jdbc:oracle:thin:@localhost:1521:assetsdb";
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 执行数据库的数据更新
public static int executeUpdata(Connection conn, String sql,
Object... parame) {
int rst = -1;
PreparedStatement pstmt = null;
try {
// 设置为手动提交更新
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
// 循环设置参数
for (int i = 0; i < parame.length; i++) {
pstmt.setObject(i + 1, parame[i]);
}
rst = pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
// 异常时,数据回滚
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return rst;
}
// 执行查询
public static ResultSet executeQuery(Connection conn, String sql,
Object... parame) {
ResultSet rs = null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for (int i = 0; i < parame.length; i++) {
pstmt.setObject(i + 1, parame[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 关闭
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null)
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int getCount(Connection conn, String sql, Object... params) {
ResultSet rs = null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
// 循环设置参数
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
if (rs.next()) {// 如果有结果返回记录条数
return rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
}
如果这样,我建议是这样的,写一个单饿独的类来进行数据库CRUD
例如:
package com.assets.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static final String user = "assets";
private static final String password = "assets";
private static final String url = "jdbc:oracle:thin:@localhost:1521:assetsdb";
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 执行数据库的数据更新
public static int executeUpdata(Connection conn, String sql,
Object... parame) {
int rst = -1;
PreparedStatement pstmt = null;
try {
// 设置为手动提交更新
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
// 循环设置参数
for (int i = 0; i < parame.length; i++) {
pstmt.setObject(i + 1, parame[i]);
}
rst = pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
// 异常时,数据回滚
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return rst;
}
// 执行查询
public static ResultSet executeQuery(Connection conn, String sql,
Object... parame) {
ResultSet rs = null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for (int i = 0; i < parame.length; i++) {
pstmt.setObject(i + 1, parame[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 关闭
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null)
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int getCount(Connection conn, String sql, Object... params) {
ResultSet rs = null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
// 循环设置参数
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
if (rs.next()) {// 如果有结果返回记录条数
return rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询