package util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;public class DBConnect {
static Connection con=null ;
private Statement stm=null;
private ResultSet rs=null;
private String sql=null;
//连接数据库
static{
String userName = "scott";
String password = "tiger";
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
con = DriverManager.getConnection(url,userName,password);
}catch (Exception e){
System.out.println("DataBase Connection Error"+e.getMessage());
e.printStackTrace();
}
} //取得数据库连接
public Connection getConnection() throws Exception{
return con;
}
/**用数组返回查询数据
* @param sql
* @return ArrayList
* @throws SQLException
*/
public ArrayList executeQuery(String sql){
this.sql = sql.trim();
ArrayList array = new ArrayList();
try{
stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(stm.execute(sql)){
rs = stm.getResultSet();
ResultSetMetaData md = rs.getMetaData();
int cols;
cols = md.getColumnCount();
while(rs.next()){
HashMap hash = new HashMap(cols);
int j;
for(j = 1;j <= cols ;j++){
Object ob = rs.getObject(j);
hash.put(md.getColumnLabel(j), ob);
}
array.add(hash);
}
rs.close();
stm.close();
return array;
}else{
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return array;
} //用结果集返回查询数据
public ResultSet executeQuery2(String sql){
try{
stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(stm.execute(sql)){
rs = stm.getResultSet();
return rs;
}else {
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return rs;
}
//关闭数据库连接
public void closeCon(){
try{
if(rs !=null){
rs.close();
}
if(stm !=null){
stm.close();
}
if(con!=null){
con.close();
}
}catch(Exception e){}
} //批处理命令返回结果集
/**public void executeBatch(ArrayList arrayList) throws SQLException{
String tempsql=null;
try{
con.setAutoCommit(false);
stm = con.createStatement();
for(int i=0;i<arrayList.size();i++){
tempsql=(String)arrayList.get(i);
System.out.println(tempsql);
stm.addBatch(tempsql);
}
stm.executeBatch();
con.commit();
}catch(SQLException e){
if (con != null) {
con.rollback();
}
throw new SQLException(e.getMessage());
}finally{
con.setAutoCommit(true);
stm.close();
}
}
public java.sql.PreparedStatement executePrecedure(String sqlstr){
java.sql.PreparedStatement prestate=null;
try {
prestate = con.prepareStatement(sqlstr);
}catch(SQLException se){
se.printStackTrace() ;
}
return prestate;
}
**/
//更新数据
public int executeUpdate(String sql) throws Exception {
this.sql = sql.trim();
int cols = 0;
try {
Statement stmt = con.createStatement();
cols = stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
throw new Exception("");
}
return cols;
}
/**public String liangwei(String shu) throws Exception {
double gweight1=new Double(shu).doubleValue();
int temp= (int)Math.round(gweight1* 100);
gweight1= (double)temp/ 100.00;
String shu1=String.valueOf(gweight1);
return shu1;
}
**/
public static void main(String[] args) {
try {
DBConnect db= new DBConnect();
ResultSet rs=db.executeQuery2("select * from PROPEL.PROPEL_INFO");
while (rs.next()) {
String aa=rs.getString("Type");
System.out.println(aa);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我要连接到我的数据库orcl。用户名:scott 密码:tiger 表:DANZHI 请问以上程序怎么改写, 用户名和密码已经改了,表怎么改?
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;public class DBConnect {
static Connection con=null ;
private Statement stm=null;
private ResultSet rs=null;
private String sql=null;
//连接数据库
static{
String userName = "scott";
String password = "tiger";
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
con = DriverManager.getConnection(url,userName,password);
}catch (Exception e){
System.out.println("DataBase Connection Error"+e.getMessage());
e.printStackTrace();
}
} //取得数据库连接
public Connection getConnection() throws Exception{
return con;
}
/**用数组返回查询数据
* @param sql
* @return ArrayList
* @throws SQLException
*/
public ArrayList executeQuery(String sql){
this.sql = sql.trim();
ArrayList array = new ArrayList();
try{
stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(stm.execute(sql)){
rs = stm.getResultSet();
ResultSetMetaData md = rs.getMetaData();
int cols;
cols = md.getColumnCount();
while(rs.next()){
HashMap hash = new HashMap(cols);
int j;
for(j = 1;j <= cols ;j++){
Object ob = rs.getObject(j);
hash.put(md.getColumnLabel(j), ob);
}
array.add(hash);
}
rs.close();
stm.close();
return array;
}else{
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return array;
} //用结果集返回查询数据
public ResultSet executeQuery2(String sql){
try{
stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(stm.execute(sql)){
rs = stm.getResultSet();
return rs;
}else {
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return rs;
}
//关闭数据库连接
public void closeCon(){
try{
if(rs !=null){
rs.close();
}
if(stm !=null){
stm.close();
}
if(con!=null){
con.close();
}
}catch(Exception e){}
} //批处理命令返回结果集
/**public void executeBatch(ArrayList arrayList) throws SQLException{
String tempsql=null;
try{
con.setAutoCommit(false);
stm = con.createStatement();
for(int i=0;i<arrayList.size();i++){
tempsql=(String)arrayList.get(i);
System.out.println(tempsql);
stm.addBatch(tempsql);
}
stm.executeBatch();
con.commit();
}catch(SQLException e){
if (con != null) {
con.rollback();
}
throw new SQLException(e.getMessage());
}finally{
con.setAutoCommit(true);
stm.close();
}
}
public java.sql.PreparedStatement executePrecedure(String sqlstr){
java.sql.PreparedStatement prestate=null;
try {
prestate = con.prepareStatement(sqlstr);
}catch(SQLException se){
se.printStackTrace() ;
}
return prestate;
}
**/
//更新数据
public int executeUpdate(String sql) throws Exception {
this.sql = sql.trim();
int cols = 0;
try {
Statement stmt = con.createStatement();
cols = stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
throw new Exception("");
}
return cols;
}
/**public String liangwei(String shu) throws Exception {
double gweight1=new Double(shu).doubleValue();
int temp= (int)Math.round(gweight1* 100);
gweight1= (double)temp/ 100.00;
String shu1=String.valueOf(gweight1);
return shu1;
}
**/
public static void main(String[] args) {
try {
DBConnect db= new DBConnect();
ResultSet rs=db.executeQuery2("select * from PROPEL.PROPEL_INFO");
while (rs.next()) {
String aa=rs.getString("Type");
System.out.println(aa);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我要连接到我的数据库orcl。用户名:scott 密码:tiger 表:DANZHI 请问以上程序怎么改写, 用户名和密码已经改了,表怎么改?
不需要改..
看你各种操作里的sql语句了:executeQuery(sql)
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}