`
qmug
  • 浏览: 197541 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JDBC使用步骤

    博客分类:
  • J2EE
阅读更多
分为6个步骤
1.
load the driver

(1)Class.forName()|Class.forName().newlnstance()|new DriverName()
(2)实例化时自动向DriverManager注册,不需要显示调用DriverManager.registerDriver

2.
Connect to the DataBase
  DriverManager.getConnection()

3.

Excute the SQL
  (1)connection.CreateStatement()

   (2)Statement.excuteQuery()

   (3)Statement.executeUpdate()

4.

Retrieve the result data

   循环取得结果 while(rs.next())

5.

show the result data
   将数据库中的各种类型转换为java中的类型(getXXX)方法

6.

close

   close the resultset / close the  statement /close the connection



实际例子


package DB;

import java.sql.*;



class  Jdbc
{
	public static void main(String[] args)throws Exception
	{
		
		//只有下面2句话就可以连接到数据库中
		Class.forName("com.mysql.jdbc.Driver");   
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234"); 
		
		
		
		//Class.forName("com.mysql.jdbc.Driver");   
		//Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
		


		//Class.forName("oracal.jdbc.driver.OracalDriver");
		//new oracal.jdbc.driver.OracalDriver();
		//Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
		
		//jdbc.driverClassName=com.mysql.jdbc.Driver;
		//jdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
	}
}


还有另外的一个用try catch 的方法


下面就实际去操作一下
首先把mysql驱动mysql-connector-java-3.1.10-bin.jar 或者其它版本的驱动copy到WebRoot 下面的WEB-INF下面的lib里面

package db;

//一定要注意类名字要相同!!


import java.sql.*;

class  Jdbc
{
	public static void main(String[] args)throws Exception
	{
		
		
		Class.forName("com.mysql.jdbc.Driver");   
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234"); 
		System.out.print("ok");//如果连接成功显示ok
	}
}


然后接着看下面的升级版
package db;

import java.sql.*;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

class Jdbc2 {
	public static void main(String[] args) throws Exception {

		//1.先new 一个driver 然后向managerDriver注册
		Class.forName("com.mysql.jdbc.Driver");
		//2.通过DriverManager.getConnection传递个方法,严格的说是jdbc的url
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/test", "root", "1234");
		//3.创建个statement对象,执行sql语句
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from test.admin");
		//4.取得结果集 5.对其进行便利
		while (rs.next()) {
			System.out.println(rs.getString("username"));
			System.out.println(rs.getInt("id"));
		}
		//6.关闭(要遵循后执行的先闭,先执行的后闭的原则)
		rs.close();
		stmt.close();
		conn.close();

	}
}

/**
 * 此例子需要注意的是:
 * 1.驱动是否在lib文件夹下面。
 * 2.数据库里面的库名以及表是否存在
 * 3."jdbc:mysql://localhost:3306/test", "root", "1234");
 * 分别对应的是地址、端口、库名、数据库的管理员名字、管理员密码。
 * 4."select * from test.admin" sql语句建议一定写的时候用 库名.表名。
 */
/*

 以上是jdbc 一个简单的例子,了解连接jdbc 的步骤。

 这段代码的统一出现的问题是在执行System.out.println(rs.getString("name"));的时候
 会出现exception,这样的话后面的close就不再被执行,占用缓存,最后导致服务器死机
 (河北电信视频点击系统)改进的代码请看TESTHdbc3.java


 */

//Class.forName("com.mysql.jdbc.Driver");   
//Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");

//Class.forName("oracal.jdbc.driver.OracalDriver");
//new oracal.jdbc.driver.OracalDriver();
//Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
//jdbc.driverClassName=com.mysql.jdbc.Driver;
//jdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
/*
以上是jdbc 一个简单的例子,了解连接jdbc 的步骤。

这段代码的统一出现的问题是在执行System.out.println(rs.getString("name"));的时候会出现exception,这样的话后面的close就不再被执行,占用缓存,最后导致服务器死机(河北电信视频点击系统)改进的代码请看TESTHdbc3.java


		
		//Class.forName("com.mysql.jdbc.Driver");   
		//Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
		


		//Class.forName("oracal.jdbc.driver.OracalDriver");
		//new oracal.jdbc.driver.OracalDriver();
		//Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
		
		//jdbc.driverClassName=com.mysql.jdbc.Driver;
		//jdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;


最后让我们看个成熟版
package db;

import java.sql.*;

class Jdbc3 {
	public static void main(String[] args) {
		try {

			Class.forName("com.mysql.jdbc.Driver");

			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "1234");

			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from test.admin");

			while (rs.next()) {
				System.out.println(rs.getString("username"));
				System.out.println(rs.getInt("id"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch(SQLException e){
			e.printStackTrace();}
		}
	finally{
		rs.close();
		stmt.close();
		conn.close();
	}
	
}

分享到:
评论
1 楼 caozhihao111 2008-07-25  
  //1.先new 一个driver 然后向managerDriver注册  
        Class.forName("com.mysql.jdbc.Driver");  
        //2.通过DriverManager.getConnection传递个方法,严格的说是jdbc的url  
        Connection conn = DriverManager.getConnection(  
                "jdbc:mysql://localhost:3306/test", "root", "1234");  
        //3.创建个statement对象,执行sql语句  
        Statement stmt = conn.createStatement();  
        ResultSet rs = stmt.executeQuery("select * from test.admin");  
        //4.取得结果集 5.对其进行便利  
        while (rs.next()) {  
            System.out.println(rs.getString("username"));  
            System.out.println(rs.getInt("id"));  
        }  
        //6.关闭(要遵循后执行的先闭,先执行的后闭的原则)  
        rs.close();  
        stmt.close();  
        conn.close();  
 
    }   

相关推荐

Global site tag (gtag.js) - Google Analytics