Mysql,Oracle,Sqlserver数据库连接

来源:互联网 发布:路基压实度灌砂法软件 编辑:程序博客网 时间:2024/06/04 18:47

.NET的Sqlserver连接串:

       'Dim constr As String = "Data Source=localhost;Initial Catalog=Hotel;user id =sa; password=123"

        '上面一句话可以在配置文件中获取

        Dim constr As String = Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

        '建立连接
        Dim conn As SqlConnection = New SqlConnection()
        conn.ConnectionString = constr

        '创建命令对象
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = conn

        cmd.CommandText = "select * from UserDetail where userName='" & namestr & "'"

        'DataReader是一种常用的数据库读取工具
        Dim reader As SqlDataReader
        Try
            conn.Open()
            reader = cmd.ExecuteReader()

            If reader.Read() Then
                If passstr = reader("userPwd") Then
                    MsgBox("登陆成功")
                Else
                    MsgBox("密码错误")
                End If
            Else
                MsgBox("没有此用户")
            End If

        Catch ex As Exception
            Throw New ApplicationException(ex.ToString())

        Finally
            reader.Close()

            conn.Close()

        End Try

    End Sub

配置文件中加上:

<connectionStrings >

<add name ="MyConnectionString" connectionString ="Data Source=localhost;Initial Catalog = Hotel; Integrated Security =SSPI"/>
</connectionStrings>

java的Sqlserver连接串:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection  conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:4720;databaseName=mydata","sa","root")

Mysql连接串:

Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
或者:"jdbc:mysql://localhost:3306/mydata","root","root"

Oracle链接:

Class.forName("oracle.jdbc.driver.OracleDriver");或者:new oracle.jdbc.driver.OracleDriver();
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:localhost:1521:orcl", "scott","orcl");

原创粉丝点击