Tomcat数据源
1 加载数据库驱动程序:
2 通过DriverManager类取得数据库的连接对象。
3.通过Connection实例化PreparedStatement对象,编写SQL命令操作数据库。
4.数据库属于资源操作,操作完成后要关闭数据库以释放资源。
对于不同的用户只有操作不同,但是对于1,2,4三个步骤很明显是一个重复的操作。
public class SelectDB
{
static final String DRIVER = "com.mysql.jdbc.Driver"; //驱动名(各个数据库不同)
static final String URL = "jdbc:mysql://localhost:3306/test"; //连接数据库的地址(各个版本的数据库不同)
static final String USER = "root"; //用户名
static final String PWD = "fendou"; //密码
/* 查询数据库 */
public static void getAll()
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//1、加载驱动
try{
Class.forName(DRIVER);
//2、使用DrvierManager得到数据库的连接
conn = DriverManager.getConnection(URL, USER, PWD);
//3、创建发送SQL语句的对象
stmt = conn.createStatement();
//4、执行SQL语句
String sql = "select id,name,age from student";
//5、用ResultSet接收查询后的结果集
rs = stmt.executeQuery(sql);
//6、循环一条一条的取出结果集的记录
while(rs.next()){//判断结果集是否有数据
//7、一列一列的取出数据(注意对应数据类型)
/*int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);*/
//一般情况都是写列名来获得该列的内容
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id+","+name+","+age);
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
try{
if(rs != null){
//5、关闭rs
rs.close();
}
if(stmt != null){
//5、关闭stmt
stmt.close();
}
if(conn != null){
//6、关闭连接
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{ getAll();
}
}
(本文摘自:http://www.cnblogs.com/200911/archive/2012/05/02/2479880.html)
相关推荐
Tomcat数据源配置Tomcat数据源配置Tomcat数据源配置Tomcat数据源配置Tomcat数据源配置Tomcat数据源配置
只有jar 和xml 没有教材.本来不想发上来的,这是权且当个中间地带吧
在TomcatWeb服务器上配置数据源, 在TomcatWeb服务器上配置数据源,
NULL 博文链接:https://chengjianxiaoxue.iteye.com/blog/2077528
NULL 博文链接:https://pioneer127.iteye.com/blog/314754
tomcat数据源的配置及使用 在eclipse中开发的示例,txt文档的说明,exe文件的教程。在使用JNDI,可分为5步 (1)配置context.xml文件(Tomcat 6.0\conf文件夹下),把连接数据库用的驱动包(sqljdbc.jar)复制到...
DbUtil和tomcat数据源配置实例
tomcat数tomcat数据源配置文档
在tomcat中配置数据源连接池的配置方法,及jar包文件。
TomCat5.5数据源配置
采用JAVA的JDBC连接池能够快速的实现网页的设计,连接池的实现主要是靠配置tomcat数据源实现的
tomcat数据源,非常全面 包括所有的数据库,包括所有的tomcat版本。
tomcat数据源配置.进行WEB开发是,既方便又安全的连接数据库方法.
tomcat数据源配置.docx ``````````` `
tomcat中配置Oracle数据源的模板,其他数据库可参考
tomcat数据源配置[总结].pdf
tomcat数据源配置[收集].pdf