日期:2014-05-16  浏览次数:20606 次

JDBC的标准连接

package com.khb.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import cn.com.infosec.asn1.ocsp.Request;

import antlr.collections.List;


public class LoginDao {

??public void searchDatabase(){

??String urlString = "jdbc:sqlserver://192.168.1.250:1433;databaseName=×××";
??String usernameString = "sa";
??String passwordString = "×××";
??String dirverString = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

??Connection conn = null;
??PreparedStatement pst = null;
??ResultSet rs = null;

??String sqlString = "select * from TB_Dinpay_Log";
??try {
???Class.forName(dirverString);
???System.out.println("类实例化、加载成功");
??? conn = DriverManager.getConnection(urlString,
?????usernameString, passwordString);
???
????? System.out.println("创建连接对象成功");
??
???
???pst = conn.prepareStatement(sqlString);
???System.out.println("创建prepareStatement成功");
???rs = pst.executeQuery();
???System.out.println("=============="+rs);
???System.out.println("数据库操作成功");
???
???ArrayList<String> list = new ArrayList<String>();
???
???while(rs.next()){
????list.add(rs.getString(1));
????list.add(rs.getString(2));
????list.add(rs.getString(3));
????list.add(rs.getString(4));
????list.add(rs.getString(5));
????
????System.out.print("? "+rs.getString(1));
????System.out.print("? "+rs.getString(2));
????System.out.print("? "+rs.getString(3));
????System.out.print("? "+rs.getString(4));
????System.out.print("? "+rs.getString(5));
????System.out.println();
???}
???
???
??} catch (ClassNotFoundException e) {
??? System.out.println("找不到驱动");
???e.printStackTrace();
??}catch (NoClassDefFoundError e) {
???System.out.println("有时是因为msutil.jar包没导进去");
???e.printStackTrace();
??}catch (SQLException e) {
???System.out.println("数据库语句异常");
???e.printStackTrace();
??}
?}
}