package day0414;
import java.sql.*;
public class DBTest1
{

Connection conn;
Statement stmt;
ResultSet rs;
String sql;
public DBTest1()
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //throws 로 하면 에러잡기 힘드니 트라이캣치쓰자
System.out.println("드라이버 성공^^*");
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("드라이버 오류:"+e.getMessage());
e.printStackTrace();
}
}
public void write()
{
try
{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/java302", "root", "302");
System.out.println("커넥션 성공이얌 쀼잉잉^_~");
} catch (SQLException e)
{
System.out.println("커넥션 실패: "+e.getMessage());
e.printStackTrace();
}
try
{
stmt=conn.createStatement(); //+++++++++++++
sql="select * from student order by num asc";
rs=stmt.executeQuery(sql);
while(rs.next())
{
int num=rs.getInt("num");
String name=rs.getString("name");
int age=rs.getInt("age");
int kor=rs.getInt("kor");
int eng=rs.getInt("eng");
Date birth=rs.getDate("birth");
String buseo=rs.getString("buseo");
System.out.println(num+"\t"+name+"\t"+age+"\t"+kor+"\t"+eng+"\t"+birth+"\t"+buseo);
}
} catch (SQLException e)
{
System.out.println("sql문 오류입니다: "+e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
{
DBTest1 db= new DBTest1();
db.write();
}

}

Posted by 커널제로

본 블로그는 페이스북 댓글을 지원합니다.

,