码迷,mamicode.com
首页 > 数据库 > 详细

java jdbc 连接 SQL server

时间:2014-12-26 16:41:46      阅读:152      评论:0      收藏:0      [点我收藏+]

标签: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

标签:java   jdbc   sql server   

原文地址:http://blog.csdn.net/chenwen_201116040110/article/details/42173201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!