How JAVA Links SQL Server databases (semi verbose)

Source: Internet
Author: User
Tags microsoft sql server management studio sql server management sql server management studio

Wow, today is nothing to be ready to put a few days before the link database to write to the blog, write to half of the browser cut out, this browser has not been saved, and wrote again at night, alas the egg hurts AH ╮(╯▽╰)╭!!

Say a connection database encountered problems, alas, the problem is many, the original brush of the lite version of the piracy system just started to open the database installation package, later brushed an original, link does not go up, and then brush the system again (delete the registration form can not be installed), some questions you need to reference ... The requirements for the framework 3.5 are high, not installed, or have problems can not be installed. Sometimes I have a VS2013, I can't fit a database, I can't fit a database. VS

!!! Ah Ah Ah

Installation issues:

Installation to this step as far as possible to choose the mixed mode of self-input password, not too complex 123456, after all, you are learning, not to maintain the database. If you install this way, you only need to perform the following steps: If this is not the case, look at the red flag after the content ... Open the SQL Configuration toolThe TCP/IP protocol OpenThat is, the following status bar is enabled. She asked to restart the service.You can click the first SQL Server service restart to start those services that are running,----------I think you should understand. You can also restart the computer AH haha!!! the second one is the port problemWe recommend using the original default port, because do not change it!! Save the Trouble! Generally that port will not be occupied, if you want to change the TCP/IP to the same port of all IPs: do not understand: here are the driver issues: Download the driver pack! https://pan.baidu.com/s/1hsb6VK4 Microsoft SQL Server JDBC Driver 3.0.zip This is Microsoft SQL Server JDBC Driver driver You can also go to the official website to download After all, the times may have updated the feature, open Eclipse or me create Java project Click on the Java project you created-right-build path-add external archive (with a small bottle icon on it) unzip the driver you downloaded, Add Sqljdbc4.jar this jar package: After you build a class copy the following code to run it
//ShijiazhuangtiedaodaxueImportJava.sql.*; Public classconn{ Public Static voidMain (string[] args)throwsException {//class.forname ("Com.mysql.jdbc.Driver"); //A database that already exists must be filled in at the beginningString url = "Jdbc:sqlserver://127.0.0.1:1433;databasename=model;"; Connection Conn= Drivermanager.getconnection (URL, "sa", "123456");//connecting to a databaseStatement stat = conn.createstatement ();//Create a Statement object to send the SQL statement to the database. ResultSet resultset=stat.executequery ("select * from master.dbo.sysdatabases where name= ' Hello '"); if(Resultset.next ()) {System.out.println ("Table exist!"); //Querying DataStat.close ();//close the original objectConn.close ();//Close the original linkurl = "Jdbc:sqlserver://127.0.0.1:1433;databasename=hello;"; Conn= Drivermanager.getconnection (URL, "sa", "123456");//Connect to database If your password is not 123456, the default SA user does not have to change it .Stat = Conn.createstatement ();//Create a Statement object to send the SQL statement to the database. ResultSet Result= Stat.executequery ("SELECT * from Sqltest");  while(Result.next ()) {System.out.println ("+result.getint" ("id") + "First Name" + result.getstring ("name") + "age" +result.getint)); }                        //Close the databaseResult.close ();            Stat.close ();                                            Conn.close (); }Else{              //Create a database HelloStat.executeupdate ("CREATE Database Hello"); //Open the created databaseStat.close ();            Conn.close (); URL= "Jdbc:sqlserver://127.0.0.1:1433;databasename=hello;"; Conn= Drivermanager.getconnection (URL, "sa", "123456");//connecting to a databaseStat = Conn.createstatement ();//Create a Statement object to send the SQL statement to the database. //Create a table sqltestStat.executeupdate ("CREATE TABLE sqltest (ID int, name varchar (), age int)"); //Add DataStat.executeupdate ("INSERT into sqltest values (20163678, ' Hu Baba ', 18)"); Stat.executeupdate (INSERT into sqltest values (20163679, ' Hemp ma ', 19) '); Stat.executeupdate (INSERT into sqltest values (20163620, ' Xi da ', 20) '); System.out.println ("CREATE TABLE Success!"); ResultSet result= Stat.executequery ("SELECT * from Sqltest");  while(Result.next ()) {System.out.println ("+result.getint" ("id") + "First Name" + result.getstring ("name") + "age" +result.getint)); }                        //Close the databaseResult.close ();            Stat.close ();        Conn.close (); }                      }            }

Inside will build a database and table for you, and read out the contents, if the table exists then read the contents directly, their own code, or novice, the code is not so streamlined ... Do not spray

If you don't create a password when you install it, you need to create a password

Point Security-Login-Open Login Properties-Follow the instructions in the diagram to change the password you need .....

Check your Login properties fourth option---status is not enabled and granted--if not changed to Enabled and granted.

then reopen SQL Server Management Studio using SQL Server Authentication login account for SA password you set it yourself. If the login succeeds, prove that your password has been successfully modified.

If the login is not successful you Baidu a bit ... I wrote it, too.

You can then copy the code to download the driver package test.

Problems encountered:

Individual classmates in my class after installing the database SQL Network configuration has multiple protocols I have only one default protocol messqlserver, they should also have a network protocol, and the database service cannot be restarted , and some display invalid remote calls (Remote call failed!). Forget alas, if you are small white suggest you reload database, because as small white they searched for a long time, also did not fix ...

Finally attach my code to run the results!!!

Show this to even on the haha!! I wish you success!

How JAVA Links SQL Server databases (semi verbose)

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.