⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaseconn.java

📁 用JAVA自己编写的人才招聘网站
💻 JAVA
字号:
package myutil;

import java.sql.*;

/**
 *负责连接sql server数据库
 */
public class DataBaseConn {
    private static Connection con;
    private Statement stmt;
    private ResultSet rs;
    private static final String drivername = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private static final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiaoyujob;user=sa;password=sa;";

    /**
     *获得到数据库得一个连接对象
     */
    public static synchronized Connection getCon() throws Exception {
        try {
            Class.forName(drivername).newInstance();
            con = DriverManager.getConnection(url);
            return con;
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            throw e;
        }
    }

    /**
     *获得一个Statement对象,只读
     */
    public Statement getStmtread() {
        try {
            con = getCon();
            stmt = con.createStatement();
            return stmt;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *获得一个结果集
     */
    public ResultSet getRs(String sql) {
        try {
            stmt = getStmtread();
            rs = stmt.executeQuery(sql);
            return rs;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *获得一个Statement对象,可以更新和insert
     */
    public Statement getStmt() {
        try {
            con = getCon();
            stmt = con.createStatement();
            return stmt;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *释放结果集rs Statement对象 关闭连接
     */
    public synchronized void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        try {
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        try {
            if (con != null) {
                con.close();
                con = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }
      public static void main(String args[])
      {
         //   String sql="delete from stu_info where number='12'";
           String sql="select * from  t_user";    
            String strsql="update stu_info set name='111',sex='111',department='111' where number='12'";
           DataBaseConn test=new DataBaseConn();
           ResultSet rs = test.getRs(sql);
           try
           {
           if(rs.next())
                 System.out.println(rs.getString("userId"));
           }catch(Exception e){};
   //        try
   //        {
     //            System.out.println("i love you!");
    //        test.execute(strsql);
      //     }catch(Exception e){};
      }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -