@Test
public void fun3() throws SQLException
{
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try{
String driverClassName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/exam";
String username="root";
String password="123";
Class.forName(driverClassName);
connection=DriverManager.getConnection(url,username,password);
/*
* 創建statement
*/
statement=connection.createStatement();
String sqlString="select * from emp";
resultSet=statement.executeQuery(sqlString);
/*
*
* 循環遍歷rs ,打印其中數據
*/
while(resultSet.next())
{
System.out.println(resultSet.getString(1)+","+resultSet.getString(2));
}
}catch(Exception e)
{
throw new RuntimeException(e);
}
finally{
//關閉資源
if(resultSet!=null) resultSet.close();
if(statement!=null) statement.close();
if(connection!=null) connection.close();
}
}
JDBC規範化寫法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.