连接oracle数据库的代码如下:

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

 

public class JdbcOracle {

 public static Connection conn=null;
 public static PreparedStatement pstamt=null;
 public static ResultSet rs=null;
 private static String driver="oracle.jdbc.driver.OracleDriver";
 private final static String url="jdbc:oralce:thin:192.168.0.109@lucs109";
 private final static String username="lucs";
 private final static String pwd="lucs";
 
 static{
  try {
   Class.forName(driver);
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public void getConn(){
  try {
   conn=DriverManager.getConnection(url, username, pwd);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("连接失败!");
  }
 }
 
 public void closeAll(){
  try{
  if(conn!=null){
   conn.close();
  }
  if(!rs.isClosed()||rs!=null){
   rs.close();
  }
  if(pstamt!=null){
   pstamt.close();
  }
  }catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 
 public int execAllSql(String sql,Object...trop){
  getConn();
  int count=0;
  try {
   pstamt=conn.prepareStatement(sql);
   for (int i = 0; i < trop.length; i++) {
    try {
     pstamt.setObject(i, trop[i]);
    
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   count=pstamt.executeUpdate();
  } catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  return count;
 }
 public static void main(String[] args) {
  JdbcOracle j=new JdbcOracle();
  j.getConn();
 }
}

相关文章:

  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-02-01
  • 2022-01-18
  • 2021-11-13
猜你喜欢
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2021-11-30
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案