단순 자바 링크 데이터베이스

1309 단어 자바sqlmysqljdbc
이 코드 의 전 제 는 MySQL 에 표를 만 드 는 것 입 니 다. 이 표 는 코드 에 설명 되 어 있 습 니 다.
초보 자가 코드 를 쓰 면 벽돌 을 찍 는 것 을 환영 합 니 다!무슨 좋 은 건의 가 있 으 면 얼마든지 말 해라.
import java.sql.*;
public class Test{
	public static void main(String[] args){
		Connection conn=null;
		Statement smt=null;
		ResultSet rs=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb?user=root&password=root");
			smt=conn.createStatement();
			rs=smt.executeQuery("select*from userinfo");
			while(rs.next()){
				System.out.print(rs.getInt("id")+"   ");
				System.out.print(rs.getString("uname")+"   ");
				System.out.println(rs.getString("upass")+"   ");
			}
		}
		catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				rs.close();
				smt.close();
				conn.close();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
}

좋은 웹페이지 즐겨찾기