`
yanshaozhi
  • 浏览: 102885 次
  • 性别: Icon_minigender_1
  • 来自: 东营
社区版块
存档分类
最新评论

久违的jdbc样板代码

阅读更多
public class DB {
	private String url = "jdbc:mysql://localhost:3306/test";

	private String user = "root";
	private String pwd = "yanshaozhi";

	public Connection getcon() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		Connection con;
		Class.forName("org.gjt.mm.mysql.Driver").newInstance();
		con = DriverManager.getConnection(url, user, pwd);
		return con;
	}

	public ResultSet executeSql(String sqlstr) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		Connection conn;
		Statement stmt;

		conn = getcon();
		stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sqlstr);

		return rs;
	}

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		DB mysql11 = new DB();
		ResultSet rs;
		rs = mysql11.executeSql("select * from user");
		try {
			while (rs.next()) {
				System.out.println(rs.getString("username"));
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics