DataBase---Intellij idea 14.1.4 using Java to connect to SQL Server tutorial

Source: Internet
Author: User
Tags stmt sql server tutorial

The Java Connection database method is broadly divided into two types: forward connection and reverse connection. The reverse connection requires the compiler to provide the relevant plug-in to support the current mainstream Java IDE supports the reverse connection. Here is a summary of the main positive connection to the experience.

I. Configuration of the database

1. Create a new login user

Log in to Database

Enter the following interface:

2. Create a user and configure user permissions

On the left of the list, select "Security" and "Login name", and right-click "New Login" to fill in the login name and password.

On the left, select server roles to configure user permissions. Permissions are given in accordance with the actual use, the last "sysadmin" is the administrator rights, generally choose this on the line.

Quit the currently logged on user, and try to see if the user you just created can log in. Create a table at the same time, and use it later when the database connection is tested.

2. Database Port Settings

Open the SQL Server Configuration Manager

Enable the SQLExpress protocol on the right

Configure TCP/IP. Double-click TCP/IP and select the IPs tab. Here I configured: the IP configuration of the IP6:127.0.0.1,ipall configuration is: 1433. (This configuration information should be remembered when the Java connection to the database is used)

3. Create a data source

Open Control Panel, administrative tools, data sources, select the System DSN tab, click Add, and select SQL Server.

Double-click SQL Server, fill in the database name and server, the description is optional.

Name yourself, the server is the name of the server when you log in to the database.

Login ID Select Log in with user input (you can choose it yourself)

Keep the next step until you're done.

4. Restart the server

Control Panel-Manage Tools-Services, select SQL Server (SQL ...), right-click, select Stop, Stop, then click Start.

6. Connect to SQL Server using IntelliJ idea

Create a new project, and then import the JDBC4.0 jar package.

JDBC Jar package is a lot of web, download down is an. exe program, run will automatically extract out two JDBC package. Note: Here we choose one on the line, not two are included, because Sqljdbc.jar this only support java1.6 and the following version, if two are included in the words will default to this, resulting in incompatibility.

Use the following code to implement a database connection:

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;/*** Created by crazyacking on 2015/6/27.*/ Public classDblinktestmain { Public Static voidMain (String args[]) {//Create A variable for the connection string.String Connectionurl = "jdbc:sqlserver://localhost:1433;" + "databasename=dblinktest;integratedsecurity=true;"; String URL= "Jdbc:sqlserver://127.0.0.1:1433;databasename=dblinktest;user=crazyacking;password=yourpassword";//SA identity ConnectionString url2= "jdbc:sqlserver://127.0.0.1:1433;databasename=dblinktest;integratedsecurity=true;";//Windows Integrated Mode connection//Declare the JDBC objects.Connection con =NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {            //establish the connection.SYSTEM.OUT.PRINTLN ("begin.")); Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Con=drivermanager.getconnection (URL); System.out.println ("End."); //Create and execute an SQL statement that returns some data.String SQL = "SELECT * FROM TestTable1"; stmt=con.createstatement (); RS=stmt.executequery (SQL); //iterate through the data in the result set and display it.             while(Rs.next ()) {System.out.println (rs.getstring (1) + "" + rs.getstring (2)); }        }        //Handle Any errors there is a occurred.        Catch(Exception e) {e.printstacktrace (); } finally {            if(rs! =NULL)                Try{rs.close (); } Catch(Exception e) {}if(stmt! =NULL)                Try{stmt.close (); } Catch(Exception e) {}if(Con! =NULL)                Try{con.close (); } Catch(Exception e) {} }}}

DataBase---Intellij idea 14.1.4 using Java to connect to SQL Server tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.