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