Eclipse连接SQLServer,进行增加操作

文章参考自:https://blog.csdn.net/vs_the_old_boy/article/details/52596350

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlServerConnect {
	public static void main(String[] args) {
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test1";
		String sql="insert into Table_1 values('50','张三风','男')";
		//String sql="select * from Table_1";
		String userName = "1141265442";
		String userPwd = "*******";
		
		//加载数据库驱动
		try {
			Class.forName(driverName);
			System.out.println("加载驱动成功!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("加载驱动失败!");
		}
		
		try {
			//获得数据库连接
			Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
			System.out.println("连接数据库成功!");
			
			//获得语句执行的对象
			Statement stmt= dbConn.createStatement();
			
			//执行语句
			ResultSet rs = stmt.executeQuery(sql);//创建数据对象
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print("SQL Server连接失败!");
		}

	}

}

 

posted @ 2018-06-06 21:29  王雪亮  阅读(1453)  评论(0编辑  收藏  举报