赞
踩
- 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连接失败!");
- }
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。