Java Eclipse Connection Database whole process

Source: Internet
Author: User
Tags stmt

1, need to download a jar package. Address Http://pan.baidu.com/s/1i50LRId

2, the code is as follows:

ImportJava.sql.*; Public classMytest { Public Static voidMain (string[] args) {String SQL= "SELECT * FROM Student"; String URL= "Jdbc:sqlserver://wanghonghong-pc\\sqlserver2;databasename=hh"; String username= "SA"; String Password= "[Email protected]";        Connection Conn;        Statement stmt;        ResultSet rs; Try{class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"). newinstance (); Conn=drivermanager.getconnection (URL, username, password); stmt=conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); RS=stmt.executequery (SQL);          System.out.println (Rs.next ());          Rs.close ();          Stmt.close ();        Conn.close (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }      }}

3. Effect:

4, and perfect one version (add output)

ImportJava.sql.*; Public classMytest { Public Static voidMain (string[] args) {String SQL= "SELECT * FROM Student"; String URL= "Jdbc:sqlserver://wanghonghong-pc\\sqlserver2;databasename=hh"; String username= "SA"; String Password= "[Email protected]";        Connection Conn;        Statement stmt;        ResultSet rs; Try{class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"). newinstance (); Conn=drivermanager.getconnection (URL, username, password); stmt=conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); RS=stmt.executequery (SQL);  while(Rs.next ()) {System.out.println (rs.getstring ("Name"));          } rs.close ();          Stmt.close ();        Conn.close (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }      }}

Ask the Gods to guide twos

Java Eclipse Connection Database whole process

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.