같은 것이 여기에 코드를
System.out.println("-------- MySQL JDBC Connection Testing ------------");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/testDb", "userName", "Password");
} catch (SQLException e) {
for(Throwable ex : e) {
System.err.println("Error occurred " + ex);
}
e.printStackTrace();
}
if (connection != null) {
System.out.println("Connected to database!");
} else {
System.out.println("Failed to make connection!");
}
try {
Statement stmt = connection.createStatement();
String query = "select * from person ;";
//person is the table name
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String name = rs.getObject(1).toString();
String gender = rs.getObject(2).toString();
System.out.println("Name of the person is " + name + " and his gender is " + gender);
//Person table has name and gender column
}
} catch (SQLException e) {
e.printStackTrace();
for(Throwable ex : e) {
System.err.println("Error occurred " + ex);
}
System.out.println("Error in fetching data");
}
주 : 나는 따라서 Java 7
및 명시 적으로 드라이버를 제공 할 필요가 올 JDBC 4
을 사용하고 있습니다. 컴파일러가 자동으로 클래스 경로에서 가져옵니다. MySqlConnector.jar을 다운로드하여 classpath에 넣을 수 있습니다. java 7을 사용하지 않는 경우 Class.forName()
을 사용하여 명시 적으로 드라이버를로드해야합니다. 물론 MySql을 사용하고 있습니다 ... 사용하는 데이터베이스로 드라이버가 변경됩니다.
희망이 도움이됩니다.
어떻게 물어 전에 빙은 어떻습니까? http://www.mkyong.com/tutorials/jdbc-tutorials/ – Reddy
그가 (내가 "JDBC"을 의미) – Karloss
@Karloss를 검색에 사용할 키워드를 알고하지 않았다 수 있음, 나도이 부결되지도 나는 어떤 공격을했다 논평. 그래서 내가 링크를 줬다. :) – Reddy