`
piperzero
  • 浏览: 3471699 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

jdbc-odbc数据源配置,及在SQLServer2005下的statement和preparedStatement实例代码

 
阅读更多

jdbc-odbc数据源配置(以SQLServer为例)

①找到“数据源”

方式1:控制面板-->管理工具-->数据源(ODBC)
方式2:在“运行”中,输入:odbcad32回车

②新建一个“数据源”

步骤1:在“用户DSN”或“系统DSN”下,选择“添加”。(如果是选择“用户DSN”,则该数据源为当前登录的用户私有;如果是“系统DSN”,则为所有用户共有)
步骤2:在弹出的对话框中,选择“SQL Server”,点击“完成”
步骤3:接下来,这里的“名称”任意;服务器要填写,你当前使用的服务器名,打开SQL Server,里面有(如图1b),我这里是:WWW-4677BA992FD\sqlexpress

图1a 步骤3图1b 步骤3
步骤4:如果你有sa用户,那么需要在这里添上你的密码

图2 步骤4
步骤5:接下去,就选择你的默认数据库名(如果你配置正确的话,这里会有你自建的数据库可选)
步骤6:下一步,下一步,直到完成,OK

下面有2个实例代码:第1个是Statement下的,第2个是PreparedStatement下的

操作说明:
①数据源名:mytest
②sa密码:sasa
③数据库名:Company
④表名:employee

PreparedStatement能将SQL语句进行预编译然后传给SQLServer数据库,减少SQLServer数据库的负担,而且还能防止“注入漏洞”。
所以,在实际开发中,我们一般使用PreparedStatement方式和SQLServer数据库互联。

jdbc-odbc下,Statement方式实例代码:

/**   
* @Title: use_statement.java
* @Package jdbc_odbc
* @Description: 在jdbc-odbc连接方式下,用statement方式执行一条查询语句
* @author 慢跑学Android
* @date 2012-1-26 下午07:49:05
* @version V1.0   
*/
package jdbc_odbc;

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

public class use_statement {

	public static void main(String[] args) {
		
		Connection ct = null;
		Statement st = null;
		ResultSet rs = null;
		
		try {
			// 1.加载数据库驱动
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			
			// 2.取得java程序与SQLServer数据库的连接
				// 此时需要指定要连接哪个数据源(如下面的mytest),如果该数据库不需要sa权限,只需这样写:
				// ct = DriverManager.getConnection("jdbc:odbc:mytest");
			ct = DriverManager.getConnection("jdbc:odbc:mytest", "sa", "sasa");
			System.out.println("数据库成功连接");
			
			// 3.用statement对象执行一条查询语句
			st = ct.createStatement();
				// 指定要操作的数据库
			st.execute("use Company");
			rs = st.executeQuery("select 员工Id,姓名 from employee");
			System.out.println("员工Id\t" + "姓名");
			while (rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				System.out.println(id + "\t" + name);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 4.释放资源			
			try {
				if (null != rs) {
					rs.close();
				}
				
				if (null != st) {
					st.close();
				}
				
				if (null != ct) {
					ct.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		

	}

}
运行结果:

jdbc-odbc下,PreparedStatement方式实例代码:

/**   
* @Title: use_preparedStatement.java
* @Package jdbc_odbc
* @Description: 在jdbc_odbc连接方式下,用preparedStatement方式执行一条查询语句
* @author 慢跑学Android
* @date 2012-1-26 下午08:22:53
* @version V1.0   
*/
package jdbc_odbc;

import java.sql.*;

public class use_preparedStatement {

	public static void main(String[] args) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		try {
			/**
			 * Get the connection to SQL Server.
			 */
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			connection = DriverManager.getConnection("jdbc:odbc:mytest","sa","sasa");
			
			/**
			 * Use preparedStatement object to execute SQL statement.
			 */
			preparedStatement = connection.prepareStatement("use Company");
			preparedStatement.execute();
			
			preparedStatement = connection.prepareStatement("select 员工Id,姓名,部门编号 from employee where 员工Id=?");
			preparedStatement.setInt(1,1008);
			
			resultSet = preparedStatement.executeQuery();
			
			System.out.println("\n员工Id\t" + "姓名\t" + "部门编号\n");
			while (resultSet.next()) {
				System.out.println(
						  resultSet.getInt("员工Id") + "\t" 
						+ resultSet.getString("姓名") + "\t"
						+ resultSet.getInt("部门编号")
						);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			/**
			 * Release the resource.
			 */
			try {
				if (null != resultSet) {
					resultSet.close();
				}
				
				if (null != preparedStatement) {
					preparedStatement.close();
				}
				
				if (null != connection) {
					connection.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


	}

}

运行结果:



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics