`
imniortal
  • 浏览: 4655 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

JDBC连接数据库例子

阅读更多
import java.sql.*;
public class jdbc {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://localhost:3306/temp";
			String user = "root";
			String password = "086123";
			
			Connection conn = DriverManager.getConnection(url, user, password);
			Statement stmt = conn.createStatement();
			String sql = "select * from people";
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				  System.out.print(rs.getString(2) + "\t");
            }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
分享到:
评论
1 楼 yulongxiang 2010-11-05  
duoxie
Global site tag (gtag.js) - Google Analytics