java連接sqlserver Spring中連接sqlserver C 連接sqlserver QT連接資料庫 ...
java連接sqlserver
1 創建 Dynamic Web Project項目
在WebContent/WEB-INF/lib中添加sqljdbc42.jar
2 在class文件里連接資料庫
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = text";
String a = "sa";
String b = "admin";
Connection conn=DriverManager.getConnection(url,"sa","admin");
PreparedStatement pstat = conn.prepareStatement(sql);
ResultSet rs = pstat.executeQuery();
---------------------------------------------------------
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
Spring中連接sqlserver
1 添加sqljdbc.jarjiabao
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.2</version>
</dependency>
2 在配置文件中連接資料庫(application.yml)
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;DatabaseName=practice
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: admin
3 操作資料庫
@Autowired
JdbcTemplate jdbcTemplate;
public List<StudentBean> getStudentByRubric(String id){
List<StudentBean> user = jdbcTemplate.query("select * from Student where rid ='"+id+"'",
new BeanPropertyRowMapper<StudentBean>(StudentBean.class));
return user;
}
4 idea中有資料庫插件可以將Microsoft SQL Server Management Studio 17中需要操作的資料庫導入idea
Database->new->Data Source->Microsoft SQL Server->Port(1433),Instance(資料庫名)->Test Connection
C#連接sqlserver
1 載入資料庫
工具->連接資料庫->Microsoft SQL Server (SqlClient)->伺服器名(登錄Microsoft SQL Server Management Studio使得伺服器名稱)->其他操作
2 連接資料庫
using System.Data.SqlClient;
string url = 伺服器資源管理器->數據連接->屬性->連接字元串
SqlConnection conn = new SqlConnection(url)
3 操作資料庫
string sql = "select * from table";
SqlCommand cmd = new SqlCommand(sql, conn)
conn.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader re = cmd.ExecuteReader();
---------------------------------------------------------------
cmd.CommandType = CommandType.Text;
result = cmd.ExecuteNonQuery();
QT連接資料庫
1 創建ODBC
打開ODBC數據資源管理程式->添加->SQL Server->伺服器(登錄SSMS的伺服器名稱)->選擇登陸的類型->選擇要添加的資料庫
2 連接ODBC
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString dsn = QString::fromLocal8Bit(創建SQL Server ODBC的名稱);
db.setHostName("127.0.0.1");
db.setDatabaseName(dsn);
db.setUserName("sa");
db.setPassword("admin");
db.open();
3 操作資料庫
QSqlQuery query(db);
query.exec("select * from Student");
db.close();