Java JDBC Connection SQL Server

Source: Internet
Author: User

About the Java JDBC Connection to SQL Server and the connection to the MySQL database is the same, in order to facilitate later viewing, the author here to give a demo.

Package Com;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.preparedstatement;import  Java.sql.sqlexception;import Java.sql.statement;public class DB {private static db _instance = Null;private static Object lock = new Object ();p ublic static DB get () {if (_instance = = null) {synchronized (lock) {if (_instance = = null) {return _instance = new DB ();}}} return _instance;} Public Connection getconnection () {try {class.forname ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");} catch (        ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}       Loads the database engine, returns the class of the given string name string Url= "JDBC:MICROSOFT:SQLSERVER://LOCALHOST:1053;DATABASENAME=FDC"; Test the name of your database as String user= "sa"; String password= "1"; Connection conn = null;try {conn = drivermanager.getconnection (Url,user,password);} catch (SQLException e) {//TODO auto-g Enerated catch Blocke.printstacktrace (); return null;} Return conn;} Public Statement getselectstmt (Connection conn) {try {REturn conn.createstatement ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}} Public PreparedStatement getinsertstmt (Connection conn,string table) {try {return conn.preparestatement ("INSERT INTO \" "+ table +" \ "VALUES (?,?,?,?,?,?,?,?,?,?)");} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}} public void Insert (PreparedStatement stmt,string rec_carid,string rec_time,string rec_longitude,string REC_LATITUDE, String rec_altitude,string rec_speed,string rec_carstatus,string rec_direction,string REC_GPSSTATUS,String DBTIME) { try {stmt.setstring (1, Rec_carid); Stmt.setstring (2, Rec_time); Stmt.setstring (3, rec_longitude); Stmt.setstring (4, Rec_latitude); stmt.setstring (5, rec_altitude); Stmt.setstring (6, rec_speed); Stmt.setstring (7, REC_CARSTATUS); Stmt.setstring (8, rec_direction); Stmt.setstring (9, rec_gpsstatus); stmt.setstring (DBTIME); Stmt.addbatch ();} catch (SQLException e) {//TODO auto-generated catch BloCke.printstacktrace ();}} public void Delete (Statement stmt,string table,int line) {try {stmt.executeupdate ("delete Top (" +line+ ") from \" "+ Table + "\"");} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Java JDBC Connection SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.