관리 메뉴

moozi

mysql jdbc 기본 본문

TIS_2018/응용sw2018_2기

mysql jdbc 기본

moozi 2018. 9. 10. 14:19

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

 

public class JDBC01 {

static Connection conn=null;

static Statement stmt=null;

static ResultSet rs=null;

 

public static void main(String[] args) {

try {

Class.forName("com.mysql.jdbc.Driver");

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb?useSSL=false", "root", "1234");

 

//mysql8.X

//Class.forName("com.mysql.cj.jdbc.Driver");

//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jwbookdb?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC", "jwbook", "1234");

//System.out.println("연결됨");

 

stmt=conn.createStatement();

 

//insert

//stmt.executeUpdate("insert into student values('0494077','왕건','컴퓨터공학')");

 

//update

//stmt.executeUpdate("update student set dept='electric' where id='1091011'");

 

//delete

stmt.executeUpdate("delete from student where id='0494077'");

 

 

rs=stmt.executeQuery("select * from student");

System.out.println("학번\t 이름\t 학과");

System.out.println("=================================");

while(rs.next()) {

System.out.print(rs.getString("id")+"\t");

System.out.print(rs.getString("name")+"\t");

System.out.print(rs.getString("dept")+"\n");

}

 

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

if(rs!=null) {

rs.close();

}

if(conn!=null) {

conn.close();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

 

}

 

 

'TIS_2018 > 응용sw2018_2기' 카테고리의 다른 글

자바연습문제17  (0) 2018.09.10
my.ini  (0) 2018.09.10
수업시간sql  (0) 2018.09.07
오라클연습문제07  (0) 2018.09.06
PL/SQL 자료  (0) 2018.09.06
Comments