package day0414;
import java.sql.*;
import java.util.Scanner;
public class DBTest2
{
Connection conn;
Statement stmt;
ResultSet rs;
String sql;
public DBTest2()
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //throws 로 하면 에러잡기 힘드니 트라이캣치쓰자
System.out.println("드라이버 성공\n============");
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("드라이버 오류:"+e.getMessage()+"\n============");
e.printStackTrace();
}
}
public void write()
{
try
{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sawon", "root", "302");
System.out.println("커넥션 성공\n============");
} catch (SQLException e)
{
System.out.println("커넥션 실패: "+e.getMessage()+"\n============");
e.printStackTrace();
}
try
{
stmt=conn.createStatement(); //+++++++++++++
sql="select * from juso order by num";
rs=stmt.executeQuery(sql);
while(rs.next())
{
int num=rs.getInt("num");
String name = rs.getString("name");
String hp = rs.getString("hp");
String address = rs.getString("address");
Date ipsaday= rs.getDate("ipsaday");
System.out.println(num+"\t"+name+"\t"+hp+"\t"+address+"\t"+ipsaday);
}
} catch (SQLException e)
{
System.out.println("sql문 오류입니다: "+e.getMessage());
e.printStackTrace();
}
System.out.println("\n\n");
//핸드폰 앞자리 3자리 입력시 같은 번호 출력(010입력시 010으로 시작되는 번호를 가진 사람출력)
System.out.print("검색할 앞부분을 입력하세요: ");
Scanner sc=new Scanner(System.in);
String n;
n=sc.nextLine();
try
{
stmt=conn.createStatement(); //+++++++++++++
sql="select * from juso where hp like '" +n+"%' order by num";
rs=stmt.executeQuery(sql);
while(rs.next())
{
int num=rs.getInt("num");
String name = rs.getString("name");
String hp = rs.getString("hp");
String address = rs.getString("address");
Date ipsaday= rs.getDate("ipsaday");
System.out.println(num+"\t"+name+"\t"+hp+"\t"+address+"\t"+ipsaday);
}
} catch (SQLException e)
{
System.out.println("sql문 오류입니다: "+e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
{
DBTest2 db= new DBTest2();
db.write();
}
}
본 블로그는 페이스북 댓글을 지원합니다.