首页 > 代码库 > java jdbc 连接 SQL server
java jdbc 连接 SQL server
关于java jdbc 连接 SQL server 和 连接 mysql 数据库是一样的,为了方便以后查看,笔者在这里给出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(); public 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 block e.printStackTrace(); return null; } //加载数据库引擎,返回给定字符串名的类 String url="jdbc:microsoft:sqlserver://localhost:1053;DatabaseName=FDC"; //test为你的数据库的名称 String user="sa"; String password="1"; Connection conn = null; try { conn = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return conn; } public Statement getSelectStmt(Connection conn) { try { return conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.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 block e.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(10, DBTIME); stmt.addBatch(); } catch (SQLException e) { // TODO Auto-generated catch block e.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 block e.printStackTrace(); } } }
java jdbc 连接 SQL server
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。