Chinaunix首页 | 论坛 | 博客
  • 博客访问: 587051
  • 博文数量: 86
  • 博客积分: 3214
  • 博客等级: 中校
  • 技术积分: 1105
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-01 18:26
文章分类

全部博文(86)

文章存档

2011年(5)

2010年(49)

2009年(3)

2008年(29)

分类: Java

2008-10-06 11:20:20

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class TestJDBC {

public static void main(String[] args) {

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/datebasename?user=username&password=password");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from datrbasename");
while(rs.next()){
//数据库操作
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}

}

阅读(903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~