我给你一个我使用好用的;
你最好最一个数据库连接类,然后在需要时创建这个连接类,或者继承。
package jp.co.nec.JTC;import java.sql.*;
import java.util.*;
import java.io.*;
public class JTCDBConnectionF implements Serializable{ /* 僐僱僋僔儑儞僆僽僕僃僋僩 */
protected Connection con = null; /* 僐儞僗僩儔僋僞 */
public JTCDBConnectionF() {
}
public void dbOpen() throws java.lang.Exception{
if (con == null || con.isClosed()) {
String url = "jdbc:oracle:oci8:@apsv";
String user = "scott";
String pass = "tiger";
// 僪儔僀僶偺儘乕僪
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// 俢俛偵愙懕
con = DriverManager.getConnection(url,user,pass);
// 僩儔儞僓僋僔儑儞僞僀僾偺愝掕
con.setTransactionIsolation( con.TRANSACTION_READ_COMMITTED );
con.setAutoCommit(false); System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
}
public JTCerrorF dbClose() {
try {
if (con != null && !con.isClosed()) {
//儘乕儖僶僢僋(惓忢廔椆帪偼close偡傞慜偵忋埵偱僐儈僢僩偡傞偙偲)
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (SQLException se) {
// return new JTCerrorF(se.getErrorCode(),se);
return new JTCerrorF("PD05",se); //DB CLOSE
}
finally {
con = null;
}
return (JTCerrorF)null;
}
}你看看这个好用吗,
这是我以前项目中用的。
你最好最一个数据库连接类,然后在需要时创建这个连接类,或者继承。
package jp.co.nec.JTC;import java.sql.*;
import java.util.*;
import java.io.*;
public class JTCDBConnectionF implements Serializable{ /* 僐僱僋僔儑儞僆僽僕僃僋僩 */
protected Connection con = null; /* 僐儞僗僩儔僋僞 */
public JTCDBConnectionF() {
}
public void dbOpen() throws java.lang.Exception{
if (con == null || con.isClosed()) {
String url = "jdbc:oracle:oci8:@apsv";
String user = "scott";
String pass = "tiger";
// 僪儔僀僶偺儘乕僪
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// 俢俛偵愙懕
con = DriverManager.getConnection(url,user,pass);
// 僩儔儞僓僋僔儑儞僞僀僾偺愝掕
con.setTransactionIsolation( con.TRANSACTION_READ_COMMITTED );
con.setAutoCommit(false); System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
}
public JTCerrorF dbClose() {
try {
if (con != null && !con.isClosed()) {
//儘乕儖僶僢僋(惓忢廔椆帪偼close偡傞慜偵忋埵偱僐儈僢僩偡傞偙偲)
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (SQLException se) {
// return new JTCerrorF(se.getErrorCode(),se);
return new JTCerrorF("PD05",se); //DB CLOSE
}
finally {
con = null;
}
return (JTCerrorF)null;
}
}你看看这个好用吗,
这是我以前项目中用的。
try{
ps.close();
conn.close();
}
catch (Exception exc){
exc.printStackTrace();
}
import java.sql.*;
import java.io.*;
public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.168:1521:imart ","system","manager");
PreparedStatement ps = conn.prepareStatement("INSERT INTO HRMACH_CONTRACT_PHOTO(ARCHIVE_CD,PHOTO) values (?,?)");
ps.setString(1,"111111134");
File inFile = new File("c:\\5.jpg");
int length = (int)inFile.length() ;
FileInputStream inFileStream =new FileInputStream(inFile);
ps.setBinaryStream(2,inFileStream,length);
ps.execute(); ps.close();//关闭ps
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally {
conn = null;
}
}