java链接mysql以及sqlserver增删改查

java利用jdbc操作数据库

MySql:mysql-connector-java-5.1.12-bin.jar

SqlService:sqljdbc4.jar

import com.sun.org.apache.xpath.internal.SourceTree;
import com.sun.xml.internal.ws.api.server.LazyMOMProvider;

import java.sql.*;

/**
 * Created by sunmig on 2016/11/7.
 */
public class SqlHelper {

    /**
     * MYSQL
     * 连接数据库必备参数
     */
    private static String driverName = "com.mysql.jdbc.Driver";
    private static String dbUrl = "jdbc:mysql://localhost:3306/ngxykjc?characterEncoding=UTF-8";
    private static String userName = "root";
    private static String userPwd = "lalala";

    /***
     * SQLSERVER信息
     */
    private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
    private static String dbUrl = "jdbc:sqlserver://localhost:1433;databaseName=数据库名";  
    private static String userName = "sa";
    private static String userPwd = "lalala";


    /**
     * 连接数据库
     */
    public static Connection getConnection() {

        try {
            Class.forName(driverName);
            Connection conn = null;
            conn = DriverManager.getConnection(dbUrl, userName, userPwd);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void createTable(String SQL) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            stmt.execute(SQL);
            System.out.println("---------- 操作成功 ----------");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("---------- 操作失败 ----------");
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 新增,更新,删除
     */
    public static void SaveOrUpdateOrDetete(String SQL) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            int rs = stmt.executeUpdate(SQL);
            if(rs > 0){
                System.out.println("---------- 操作成功 ----------");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("---------- 操作失败 ----------");
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *  查询
     */

    public void Query(String SQL) {

        Connection conn = null;
        Statement stmt = null;
        try{
            conn = getConnection();
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            while (rs.next()) {
                System.out.println(rs.getString("字段名"));
            }

        } catch (SQLException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public ResultSet getResults(String SQL) {

        Connection conn = null;
        Statement stmt = null;
        try{
            conn = getConnection();
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            return rs;

        } catch (SQLException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

你可能感兴趣的:(JAVA基础知识,数据库)