DOC

JDBC

By Martha Turner,2014-07-04 19:53
10 views 0
JDBC

     Jdbc连接数据库

    import java.io.IOException;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.Statement;

    import java.util.Properties;

    public class DBConnection extends Properties {

     Connection conn;

     Statement sm;

     ResultSet rs;

     static {

     try {

     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

     } catch (ClassNotFoundException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

     public Connection openConnection() {

     try {

     conn = DriverManager.getConnection(

     "jdbc:sqlserver://localhost:1433;databasename=数据库名称",

     "登陆名", "登陆密码");

     return conn;

     } catch (Exception e) {

     e.printStackTrace();

     return null;

     }

     }

     public Statement openStatement() {

     try {

     if (conn == null || conn.isClosed())

     conn = openConnection();

     sm = conn.createStatement();

     return sm;

     } catch (Exception e) {

     // TODO: handle exception

     e.printStackTrace();

     return null;

     }

     }

    // 返回结果集 执行查询用

     public ResultSet openResultSet(String sql) {

     try {

     if (sm == null)

     sm = openStatement();

     rs = sm.executeQuery(sql);

     return rs;

     } catch (Exception e) {

     // TODO: handle exception

     e.printStackTrace();

     return null;

     }

     }

    //执行 插入 更新 删除

     public int execute(String sql) {

     try {

     if (sm == null)

     sm = openStatement();

     int result = sm.executeUpdate(sql);

     return result;

     } catch (Exception e) {

     // TODO: handle exception

     e.printStackTrace();

     return -1;

     }

     }

     public void clossAll() {

     try {

     if (rs != null)

     rs.close();

     if (sm != null)

     sm.close();

     if (conn != null && !conn.isClosed())

     conn.close();

     } catch (Exception e) {

     // TODO: handle exception

     e.printStackTrace();

     }

     }

    }

    回答人的补充 2009-06-21 23:18

    "数据库名称","登陆名", "登陆密码"换成你自己的 如果你做查询就 调用 openResultSet(String sql)方法

    如果你做插入、更新、删除就调用execute(String sql)方法

    记得做完后关闭数据库 调用clossAll() 方法 这个就是sql2005JDBC直连,不需要你做任何修改~~~

import java.sql.*;

public class LinkDB {

     Connection con=null;

     Statement st=null;

     ResultSet rs=null;

     /*String url="jdbc:microsoft:sqlserver://localhost:1433;databasename=SuperMarket";

     String user="sa";

     String psw="sa";*/

     public LinkDB(){

     try {

     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

     con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename

    =SuperMarket;","sa","sa");

     st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UP

    DATABLE);

     } catch (ClassNotFoundException e) {

     e.printStackTrace();

     }catch(SQLException e){

     System.out.println("连接数据库错误~~~~请检测:");

     e.printStackTrace();

     }

     }

     public ResultSet execQuery(String sql){

     try {

     rs=st.executeQuery(sql);

     } catch (SQLException e) {

     e.printStackTrace();

     }

     return rs;

     }

     public int execUpdate(String sql){

     int i=0;

     try {

     i=st.executeUpdate(sql);

     } catch (SQLException e) {

     e.printStackTrace();

     }

     return i;

     }

     public void Close(){

     try {

     if(rs!=null){

     rs.close();

     }

     st.close();

     con.close();

     } catch (SQLException e) {

     e.printStackTrace();

     }

     }

     public static void main(String arg []){

     System.out.println("ssssssssss");

     }

Report this document

For any questions or suggestions please email
cust-service@docsford.com