`
oogchoo
  • 浏览: 534 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
文章分类
社区版块
存档分类
最新评论

简单Java链接数据库

阅读更多
这个代码的前提是在MySQL中建了个表,这个表在代码中已经有所说明。
新手写代码,欢迎拍砖!有什么好的建议的尽管说出来。
import java.sql.*;
public class Test{
	public static void main(String[] args){
		Connection conn=null;
		Statement smt=null;
		ResultSet rs=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb?user=root&password=root");
			smt=conn.createStatement();
			rs=smt.executeQuery("select*from userinfo");
			while(rs.next()){
				System.out.print(rs.getInt("id")+"   ");
				System.out.print(rs.getString("uname")+"   ");
				System.out.println(rs.getString("upass")+"   ");
			}
		}
		catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				rs.close();
				smt.close();
				conn.close();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics