/**/
/*
* 获取最大程度的通用性,这里提供了一个思路
*
* */
import java.sql. * ;
public class mysql ... {
public static void main(String[] args) ...{
String connstring="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/bbs?user=root&password=";
String sql = "select * from article";
String columnIndex="cont";
MySQL(connstring,url,sql,columnIndex);
}
public static void MySQL(String connstring,String url,String mysql,String columnIndex) ...{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try ...{
Class.forName(connstring);
stmt = conn.createStatement();
rs = stmt.executeQuery(mysql);
while (rs.next()) ...{
System.out.println(rs.getString(columnIndex));
}
} catch (ClassNotFoundException e) ...{
e.printStackTrace();
} catch (SQLException e) ...{
e.printStackTrace();
} finally ...{
try ...{
if (conn != null) ...{
conn.close();
conn = null;
}
if (rs != null) ...{
rs.close();
rs = null;
}
if (stmt != null) ...{
stmt.close();
stmt = null;
}
} catch (SQLException e) ...{
e.printStackTrace();
}
}
}
}
* 获取最大程度的通用性,这里提供了一个思路
*
* */
import java.sql. * ;
public class mysql ... {
public static void main(String[] args) ...{
String connstring="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/bbs?user=root&password=";
String sql = "select * from article";
String columnIndex="cont";
MySQL(connstring,url,sql,columnIndex);
}
public static void MySQL(String connstring,String url,String mysql,String columnIndex) ...{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try ...{
Class.forName(connstring);
stmt = conn.createStatement();
rs = stmt.executeQuery(mysql);
while (rs.next()) ...{
System.out.println(rs.getString(columnIndex));
}
} catch (ClassNotFoundException e) ...{
e.printStackTrace();
} catch (SQLException e) ...{
e.printStackTrace();
} finally ...{
try ...{
if (conn != null) ...{
conn.close();
conn = null;
}
if (rs != null) ...{
rs.close();
rs = null;
}
if (stmt != null) ...{
stmt.close();
stmt = null;
}
} catch (SQLException e) ...{
e.printStackTrace();
}
}
}
}