Eclipse에서 oracle 연결하기
SELECT *
package jdbc_concept;
import java.sql.*;
import java.util.Scanner;
public class ConnectionTest {
public static void main(String[] args) {
/*
* 1.Driver 실행
* 다른쪽에서 에러가 날 수 있기때문에 try catch 해준다.
* */
try {
Class.forName("oracle.jdbc.OracleDriver");
//이건 스태틱이라 new하지 않는다.
} catch (ClassNotFoundException e) {
System.out.println("드라이버 클래스를 찾지 못했습니다.");
e.printStackTrace();
}
//2. DB 연결
String user = "oracle";
String password = "oracle";
/*
* @localhost : 데이터베이스 서버(프로그램이 설치된 컴퓨터의 IP주소)
* 1521 : 데이터베이스 Port Number(포트번호)
* IP주소 : 컴퓨터주소,버전4
* 127.0.0.1 : LoopBack, localhost 다 같은 의미. 자기자신
* 다른 컴퓨터와 통신을 하겨면 그 컴퓨터에 IP 주소를 가져온다.
* DNS구축하면 도메인으로 ip를 쓸 수 있다.
* xe : db의 이름 버젼에 따라 달라질 수 있다.
* */
String url = "jdbc:oracle:thin:@localhost:1521:xe";
Connection con = null;
try {
con = DriverManager.getConnection(url, user, password);
System.out.println("DB 연결성공");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3. 데이터베이스로 쿼리문 전달
Scanner sc = new Scanner(System.in);
System.out.print("아이디 : ");
String id = sc.next();
//String sql = "select * from test where id ='"+ id +"'";
String sql = "select * from test where id =?";
//쿼리문을 전달해서 디비의 데이터를 가져온다.
PreparedStatement ps = null;//보안기능도 있다. 데이터를 받을곳에 ?
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
ps.setString(1,id);//첫번째 물음표에 들어갈 값
//실행 : SELECT
rs = ps.executeQuery();
if(rs.next()) {
//데이터가 있는지 없는지 boolean으로 반환
System.out.println("번호 : "+rs.getInt("num"));
System.out.println("아이디 : "+rs.getString("id"));
System.out.println("비밀번호 : "+rs.getString("password"));
System.out.println("이름 : "+rs.getString("name"));
}else System.out.println("검색한아이디로 정보를 확인 할 수 없습니다.");
//실행 : INSERT UPDATE DELETE 명령 후에 행을 반환 해 준다.
//int row = ps.executeUpdate(); //잘되면 1을 반환
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
UPDATE
package jdbc_concept;
import java.sql.*;
import java.util.Scanner;
public class Update {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "oracle";
String password = "oracle";
String sql = "SELECT count(id) From test where id=?";
System.out.print("아이디 : ");String id=sc.next();
try {
con = DriverManager.getConnection(url,user,password);
ps=con.prepareStatement(sql);
ps.setString(1, id);
rs=ps.executeQuery();
if(rs.next()) {
int row = rs.getInt(1);
if(row==1) {
System.out.print("비밀번호 : ");String pass=sc.next();
System.out.print("이름 : ");String name=sc.next();
sql = "update test set password=?,name=? where id=?";
ps=con.prepareStatement(sql);
ps.setString(1,pass);
ps.setString(2,name);
ps.setString(3,id);
ps.executeUpdate();
}System.out.println("등록 정보가 아닙니다");
}
}catch(SQLException e){
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
행의 최대값 구하기
public int selectMaxnum() {
//sql = "select nvl(max(num),-1) from jdbs_concept5";
//max의 값을 제대로 얻어오지 못했을때 -1을 반환한다. 제대로 받아왔다면 max의 값이 반환된다.
sql = "select max(num)) from jdbs_concept5";
int maxNum = 0;
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
maxNum=rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}
return ++maxNum;
}
커넥션 닫기
커넥션은 실행한 반대로 닫아준다.
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
}
'코딩 공부 > Database' 카테고리의 다른 글
Oracle SQLPLUS 명령어 모음 (0) | 2022.09.10 |
---|---|
DATABASE SQLPLUS (0) | 2022.09.06 |