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