jdbc
Edit참조하는 jar파일 확인
pmap [pid] | grep jar
혹은 cat /proc/<pid>/smaps
ps axuw | grep java | grep -v grep | awk '{print $2}' | while read P; do pmap $P | grep 'spring' ; done
- Apache MaxClients와 Tomcat의 Full GC
- JDBC internal timeout의 이해
- Garbage Collection과 Statement Pool
Statement Cache
- https://vladmihalcea.com/how-does-the-mysql-jdbc-driver-handle-prepared-statements/
- https://dev.mysql.com/doc/refman/5.6/en/statement-caching.html
JDBC API 사용법
- 서블렛 + JDBC 연동시 코딩 고려사항 -제1탄-
- 서블렛 + JDBC 연동시 코딩 고려사항 -제2탄-
- 서블렛 + JDBC 연동시 코딩 고려사항 4
- 서블렛 + JDBC 연동시 코딩 고려사항 5
- 서블렛 + JDBC 연동시 코딩 고려사항 6
Statment를 안 가지면 maximum open cursor exceed ! 에러나 Limit on number of statements exceeded 에러 발생
- Top Ten Oracle JDBC Tips
- JDBC 드라이버의 4가지 타입 : http://www.onjava.com/pub/a/onjava/excerpt/javaentnut_2/index1.html
각종 DBMS JDBC 드라이버 셋팅법 정리
에러 관련
- Oracle Protocol-violation
- ORA-01000: maximum open cursors exceeded 조사
- ResultSet 의 close 메소드를 finally 에서 반드시 부르지 않아도 되는 이유
- JDBC 중복할당에 의한 WAS행(Hang)현상 추적하기
Mysql
- https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-configuration-properties.html
- https://kwonnam.pe.kr/wiki/database/mysql/jdbc
- MySQL에서는
useServerPrepStmts=true를써야 Server side cache 활성화됨 (default false)
- MySQL에서는
Fetch size
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-implementation-notes.html
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html#setFetchSize(int)
http://www.databasesandlife.com/reading-row-by-row-into-java-from-mysql/
connector 버전 5.0.2이상에서는 useCursorFetch가 먹음 : http://wiki.gxtechnical.com/commwiki/servlet/hwiki?Client+and+server+cursors+-+using+MySQL
autoreconnect=true
Mysql batchupdate
BLOB image 관련
이미지 저장
테이블 생성
create table test(
img blob
);
import sun.misc.*;
import java.sql.*;
import java.io.*;
public class test
{
public static void main(String args[])
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "xxx", "xxx");
con.setAutoCommit(false);
File file = new File("Sample.jpg");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = con.prepareStatement("insert into test (img) values (?)");
ps.setBinaryStream(1, fis, (int)file.length());
ps.executeUpdate();
ps.close();
con.close();
fis.close();
} catch(Exception e) {
System.out.println(e.toString());
}
}
}
DB에서 이미지 읽어서 웹으로 보여주기 JSP
<%@ page import="java.sql.*, java.io.*"%><%
String driver = "com.sybase.jdbc3.jdbc.SybDriver";
String strUrl = "jdbc:sybase:Tds:11.22.1.111:1111/Dbname";
// jdbc db local port sid.
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs;
ByteArrayOutputStream os = null;
byte[] result = null;
try{
Class.forName(driver);
}catch(Exception exception) {
System.out.println(exception);
}
try{
Properties p=new Properties();
p.put("user","ebaiusr");
p.put("password","****");
conn = DriverManager.getConnection(strUrl,p);
// 디비에서 읽는다.
pstmt = conn.prepareStatement("select PIC_FILE from PIC_FILE where RCN = '780728' ");
rs = pstmt.executeQuery();
if(rs.next()) {
InputStream is = rs.getBinaryStream(1);
os = new ByteArrayOutputStream();
int i;
while((i = is.read()) != -1) {
os.write(i);
}
result = os.toByteArray();
is.close();
os.close();
}
} catch(Exception e) {
out.println("error sql execute");
out.println("result = " + e.toString());
}
finally
{
try {
if(conn != null) conn.close();
}
catch(Exception e) {
out.println("error conn close");
}
}
response.setContentType("image/jpeg");
ServletOutputStream oout = response.getOutputStream();
oout.write(result);
oout.flush();
oout.close();
%>
기타
- [DB 가벼운 SQL 인터페이스..]
- http://java.dzone.com/articles/lightweight-sql-interfaces-jav
JDBC codes
DB메타정보 알기
import java.io.*;
import java.util.*;
import java.sql.*;
public class Test2 {
public static void main(String[] args) throws SQLException, FileNotFoundException {
try{
try{
Class.forName ("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("");
Statement stmt = null;
DatabaseMetaData dbm= con.getMetaData();
System.out.println("Product Name :"+dbm.getDatabaseProductName());
System.out.println("Product Version :"+dbm.getDatabaseProductVersion());
System.out.println("Driver Major Version :"+dbm.getDriverMajorVersion());
System.out.println("Driver Minor Version :"+dbm.getDriverMinorVersion());
System.out.println("Driver Name :"+dbm.getDriverName());
System.out.println("Driver Version :"+dbm.getDriverVersion());
} catch (SQLException ex) {
System.out.println("BulletinList:execList() : SQLException") ;
System.out.println("SQLState : " + ex.getSQLState()) ;
System.out.println("message : " + ex.getMessage()) ;
System.out.println("Oracle Error Code : " + ex.getErrorCode() ) ;
}
}catch (Exception ee) {
System.out.println("haha"+ee.getMessage());
}
}
}
JSP에서 DataSource를 이용한 Connection 획득
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
...
Context ctx = new InitialContext();
// 이 객체가 JNDI에 미리 등록되어 있어야만 한다.
DataSource ds = (DataSource)ctx.lookup("jdbc/bookSampleDB");
Connection con = ds.getConnection("id", "password");
(JSP에서)
<%@ page contentType = "text/html; charset=euc-kr" language="java" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "javax.naming.*" %>
<%@ page import = "javax.sql.*" %>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context ctx = new InitialContext();
// 이 객체가 JNDI에 미리 등록되어 있어야만 한다.
DataSource ds = (DataSource)ctx.lookup("sybaseTXDS");
Connection con = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT USR_ID, NAME, RCN, EMPLNUM FROM CM_USRINFO WHERE BAI_USR_YN = 'Y'");
%>
각종 Connection 획득방법
public class DBHelper {
private Connection connection = null;
public Connection getConnection(){
return connection;
}
public DBHelper(String driver,String url,String user, String password){
this(driver,url,user, password, null);
}
public DBHelper(){ }
public void setConnectionFromDataSource(String dataSourceName) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dataSourceName);
connection = ds.getConnection();
}
public DBHelper(String propertyFileName){
try {
Properties prop = new Properties();
prop.load(new FileInputStream(propertyFileName));
setConnection(prop);
} catch(Exception e) {
e.printStackTrace();
}
}
private void setConnection(Properties prop){
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
try{
Class.forName(driver);
} catch(Exception e){
e.printStackTrace();
}
try{
connection = DriverManager.getConnection(url,prop);
System.out.println("Connection Success");
} catch(Exception e) {
e.printStackTrace();
}
}
public DBHelper(String driver,String url,String user, String password, String charSet){
Properties prop = new Properties();
prop.put("driver",driver);
prop.put("url",url);
prop.put("user",user);
prop.put("password",password);
if ( charSet != null) prop.put("charSet",charSet);
setConnection(prop);
}
DB to DB insert
Oracle Clob, Blob
batchUpdate
private static void batchUpdate(String url, String driver, Properties prop, String query) {
Connection con = null;
PreparedStatement psmt = null;
try{
Class.forName (driver);
con = DriverManager.getConnection(url,prop);
con.setAutoCommit(false);
psmt = con.prepareStatement(query.toString());
for(int i=1;i<=1000;i++){
psmt.setString(1, "00");
psmt.setString(2, String.valueOf(i));
psmt.setString(3, "code:"+i);
psmt.addBatch();
System.out.println(i+"th row set");
if(i%100==0) {
psmt.executeBatch();
System.out.println("commit");
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
DbUtils.closeQuietly(psmt);
DbUtils.closeQuietly(con);
}
}