Home >Computer Tutorials >Computer Knowledge >How to connect to SQL database using ODBC data source
1. Load the ODBC jar package and build the path
2. Write the connection program:
public Connection getConnection(){
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //Load database driver
System.out.println ("Database driver loading completed!"); //Driver loading successful test
String url = "jdbc:oracle:thin:@localhost:1521:dbName"; //The connection url dbName is the actual database name
String username = "system"; //Database connection username
String password = "123456"; //Password
Connection con = DriverManager.getConnection(url,username,password); //Connect to the database
if(con != null){
System.out.println ("Connection successfully established with Oracle database!"); //Successful connection test
}
}catch(Exception e){
e.printStackTrace(); //Exception handling
}
return conn; //return connection
}
3. When you want to establish a connection, just call the method getConnection(). What is returned is a conn connection
1. Configure ODBC data source
1. In the Control Panel, double-click Management Tools and then open the ODBC Data Source Manager.
2. In the "System DSN" tab, click the "Add" button to open the "Create New Data Source" dialog box, and select "SQL Server" in the "Name" list box. Select and click Finish
3. When opening the "Create a new data source to SQL Server" dialog box, enter the name of the new data source in the "Name" text box, and describe the data source in the way you understand it (optional). "Server" selects the server you want to connect to.
4. Select SQL verification using the user’s login ID and password.
. Select connection SQL default settings
5. Next step, next step, complete. Just test the data source to see if the connection is successful. Press OK when successful.
2. Setting up the connection in VB
1. Add the component Microsoft ADO Data Control 6.0 (OLEDB) and drag the component to the form.
2. Right-click the ADO component and select Properties, select Use Connection String, and click Generate.
3. Select Mircosoft OLE DB Provider for SQL Server
Press next
4、
1. Enter the server name
2. Use specified server information
3. Select the database on the server
At this time, just select the database you built in SQL Server
5. Test the connection to see whether the connection is successful!
Take configuring the ODBC data source of SQL SERVER under WINDOWS XP as an example
The first step is to select Management Tools--ODBC Data Source under the control panel and double-click the icon
Step 2: On the user DSN tab, click the "Add" button. The Create New Data Source dialog box will appear. Select SQL Server
The third step is to enter the data source name, data source description and the server name or IP address where SQL Server is located. The server name can be the name of the machine where SQL Server is located, or it can be the IP address. Click the Next button.
Step 4 Select the authentication method when logging into SQL Server. And enter the username and password used to log in to SQL Server. The username and password here are established in SQL Server.
Step 5: Fill in the database name when changing the default database, and click Next.
Step six, click Finish and test the data source. After the test is successful, the creation of the ODBC data source is completed.
The above is the detailed content of How to connect to SQL database using ODBC data source. For more information, please follow other related articles on the PHP Chinese website!