笔试题:写一个mysql、Oracle、sqlserver(可选)数据库连接

搞忘了怎么写

3个回答

先写一个通用的数据库连接类
[code="java"]public class CommonConst
{
//数据库连接常量
public static String DB_DRIVER_CLASSNAME="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String public static String DB_CONN_STRING = "jdbc:sqlserver://localhost:1433;DatabaseName=xxx;user=xx;password=xx";
[/code]
}

连接数据库方法
[code="java"]
try
{
//载入SQLServer的JDBC驱动类
Class.forName(CommonConst.DB_DRIVER_CLASSNAME);
//获得数据库连接
Connection conn = DriverManager.getConnection( CommonConst.DB_CONN_STRING );

        Statement stmt = null;
        ResultSet rs = null;

        String sUsername = (String)mySession.getAttribute("username");

        try
        {
            //准备检索数据库
            stmt = conn.createStatement();

            //获取数据库中地址簿中的所有记录
            Vector vAddresses = new Vector();
            String sQuery = "select * from address where username='" + sUsername + "' order by friend_name";
            rs = stmt.executeQuery( sQuery );
            while ( rs.next() )
            {
                String sFriendName      = rs.getString(2);
                String sFriendMail     = rs.getString(3);

                MailAddress ma = new MailAddress();
                ma.setFriendName(sFriendName);
                ma.setFriendMail(sFriendMail);

                vAddresses.add( ma );
            }

            myValues.put( "addresses", vAddresses );
        }
        catch(Exception e)
        {
            conn.rollback();
            e.printStackTrace();
            mySession.setAttribute("errMsg","查找地址簿时出现错误!");
            return false;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
            }catch(Exception ex)
            {
            }
        }
    }catch(Exception ex)
    {
        ex.printStackTrace();
        mySession.setAttribute("errMsg","查找地址簿时出现错误!");
        return false;
    }

    return true;
}

[/code]

[code="java"]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MySQLConnDemo {
// private static String driverClass;
private static String url;
private static String user;
private static String password;

static {
    // driverClass = "com.mysql.jdbc.Driver";
    url = "jdbc:mysql:///information_schema";
    user = "username";
    password = "yourpassword";
    // 使用JDBC4的驱动,以下的内容可以省略,因为采用了JDK6的ServiceLoader,所以DriverManager.getConnection
    // 方法得到了增强,可自动加载 JDBC Driver。
    // 检查驱动jar包下的META-INF/services/java.sql.Driver是否存在,即可验证是否是JDBC4类型的驱动
    // try {
    // Class.forName(driverClass);
    // } catch (ClassNotFoundException e) {
    // System.out.println("无法找到驱动类,请检查类名以及类路径是否正确!");
    // throw new RuntimeException("无法找到驱动类");
    // }
}

public static void main(String[] agrs) throws Exception {

    Connection conn = DriverManager.getConnection(url, user, password);
    Statement st = conn.createStatement();
    ResultSet rs = null;
    try {
        rs = st.executeQuery("SELECT SCHEMA_NAME FROM SCHEMATA");
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } finally {
        rs.close();
        st.close();
        conn.close();
    }
}

}
[/code]

你都可以上网发问题了,为什么不百度一下,多的是答案

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐