欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

连接 sql

程序员文章站 2022-12-21 15:53:51
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();