자바 DB사용 샘풀 > 학습자료 | IOTsw_u2 U2 Project
학습자료

SQL 자바 DB사용 샘풀

본문

​Register

 

 

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Register{
	


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con = null;
		Statement stmt = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","smrit","oracle");
			stmt = con.createStatement();
			
			String sql="";

			//번호 아이디 페스워드 이름 나이 부서 회사이름 주소 전화 헨드폰
			//직접넣기
			//sql = "insert into u2_member values(1,'u2','u2','길남',18,'초입생','u2Solution','청천동','02-231-2332','010-3433-52335')";
			
			//시퀀스 이용해서 넣기
			sql = "insert into u2_member values( seq_U2_Member.nextval,'U2','U2PASS','개똥',10,'실장','U2','인천','02-111-2222','010-5555-5555')";
			
			int result = stmt.executeUpdate(sql);
            System.out.println(result + "개의 행이 추가 되었습니다");
            
		    }catch(ClassNotFoundException e){
			e.printStackTrace();	
		    }catch(SQLException e){
			e.printStackTrace();				
		    }finally{
			try{if(stmt !=null)stmt.close(); }catch(SQLException e){}
			try{if(con !=null)con.close(); }catch(SQLException e){}
		  }
		}

	}

 

 

 

 

List 

 

 

 

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

public class List {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection con = null; //sql와 커넥션함
  Statement stmt = null; //sql문 받아옴
  ResultSet rs=null;
  try{
	   Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc6.jar를 사용하겠다 선언
	   con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "smrit", "oracle");
	   //연결하기 위한 객체 생성(@localhost:자신의 컴퓨터:DB의SID)
	   stmt = con.createStatement();
	   
	   String sql = "";
	   sql = "select * from u2_member";   
	   rs = stmt.executeQuery(sql);
	   //몇개의 행이 추가 됬는지...
	   while(rs.next()){
		   System.out.print(rs.getString(1)+"/");
		   System.out.print(rs.getString(2)+"/");
		   System.out.print(rs.getString(3)+"/");
		   System.out.print(rs.getString(4)+"/");
		   System.out.print(rs.getString(5)+"/");
		   System.out.print(rs.getString(6)+"/");
		   System.out.print(rs.getString(7)+"/");
		   System.out.print(rs.getString(8)+"/");
		   System.out.print(rs.getString(9)+"/");
		   System.out.println();
		   //break;
	   }
  }catch(ClassNotFoundException e){
	  System.out.println("class가 없습니다");
  }catch(SQLException e){
	  
  }finally{
   try{
    if(stmt != null)
     stmt.close(); //테이블 닫음
   }catch(SQLException e){}
   try{
    if(con != null)
     con.close(); //DB접속 끊음
   }catch(SQLException e){}
  }
 }

} 

 

 

 

Update 

 

 

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Update {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con = null;
		Statement stmt = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","smrit","oracle");
			stmt = con.createStatement();
			
			String sql="";
			sql = "UPDATE u2_member set name = 'hhhh' where name='gwon'";
			int result = stmt.executeUpdate(sql);
            System.out.println(result + " 건이 실행되었습니다");
            
		    }catch(ClassNotFoundException e){
			e.printStackTrace();	
		    }catch(SQLException e){
			e.printStackTrace();				
		    }finally{
			try{if(stmt !=null)stmt.close(); }catch(SQLException e){}
			try{if(con !=null)con.close(); }catch(SQLException e){}
		  }
		}

	}

 

 

댓글목록

학습자료 목록

Total 91건 1 페이지
게시물 검색

IOTsw_u2 정보

회사 . U2
주소 . 어느별 하늘 아래에 있것지요
사업자 등록번호 . 백수임 대표 . 김씨 전화 . 02-123-4567 팩스 . 팩스없음
통신판매업신고번호 . 낼할께 개인정보관리책임자 . 김씨가 알아서 함 부가통신사업신고번호 신고안함
Copyright © 2001-2013 U2. All Rights Reserved.
닫기