• SQL SERVER 连接 标准 2005 --sqljdbc4.jar


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;

    import org.omg.CORBA.PRIVATE_MEMBER;

    /**
    * @author 郑水洪
    * SQL SERVER 2005连接
    * 端口默认是:1433 这里是1434
    *
    * 在内存中,只需要加载一次数据库驱动即可,所以应该放到static代码快中 (优化性能)
    */
    public class JdbcUtil {

    private static String url = "jdbc:mysql://localhost:3306/test";
    private static String userName = "root";
    private static String password ="root";
    private static String driverName = "com.mysql.jdbc.Driver";

    //1.加载数据库驱动
    static{
    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    // TODO: handle exception
    e.printStackTrace();
    System.out.println("数据库加载驱动失败!");
    }
    }

    //2.建立连接
    public static Connection getConnection(){
    Connection connection = null;
    try {
    connection = DriverManager.getConnection(url, userName, password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return connection;
    }
    //3.申明sql执行器

    //4.执行SQL 操作数据

    // 5、关闭资源
    public static void free(ResultSet resultSet,Statement statement,PreparedStatement preparedStatement,Connection connection){
    try{
    if (resultSet!=null) {
    resultSet.close();
    }
    }catch (SQLException e) {
    e.printStackTrace();
    }finally{
    try {
    if(statement!=null){
    statement.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    try {
    if (preparedStatement!=null) {
    preparedStatement.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    try {
    if (connection!=null) {
    connection.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    //query
    // public static ArrayList<Object> query(String sql,String colum){//应该用反射
    //
    // Connection connection = null;
    // ResultSet resultSet = null;
    // PreparedStatement preparedStatement =null;
    //
    // try{
    // connection = getConnection();
    // //SQL执行器
    // preparedStatement = connection.prepareStatement(sql);
    // resultSet =preparedStatement.executeQuery();
    // while (resultSet.next()) {
    // resultSet.getString(colum);
    // }
    // }catch(Exception e){
    // e.printStackTrace();
    // }finally{
    //
    // }
    // return null;
    // }

    //query 字段
    public static String query(String sql,String colum){//应该用反射

    Connection connection = null;
    ResultSet resultSet = null;
    PreparedStatement preparedStatement =null;
    String str ="";

    try{
    connection = getConnection();
    //SQL执行器
    preparedStatement = connection.prepareStatement(sql);
    resultSet =preparedStatement.executeQuery();
    while (resultSet.next()) {
    str = resultSet.getString(colum);
    }
    }catch(Exception e){
    e.printStackTrace();
    }finally{

    }
    return str;
    }

    public static void main(String[] args) {
    String sql =" select * from demo r where r.id=1";
    String colum = "name";
    System.out.println(query(sql,colum));
    }
    }

  • 相关阅读:
    44、通配符匹配
    Consul注册中心
    16.11、跳水板
    209、长度最小的子数组
    分布式CAP定理
    剑指 Offer 09、用两个栈实现队列
    git 2.33+版本升级后sshrsa认证失效问题
    OpenJDK各平台版本下载
    maven阿里云镜像无法下载gexinrpsdkbase
    2.SpringBoot集成MinIo文件服务器
  • 原文地址:https://www.cnblogs.com/zshboke-2015/p/5025691.html
Copyright © 2020-2023  润新知