Java Connection Mysql,sql Server database

Source: Internet
Author: User
Tags java web

Java connection MySQL:

First import the jar package file:

  

: http://download.csdn.net/detail/chongzi321/5703641

And then:

    

1Connection ct =NULL; 2Statement SM =NULL; 3ResultSet rs =NULL;4           5         Try{     6Class.forName ("Com.mysql.jdbc.Driver"); 7ct = drivermanager.getconnection ("jdbc:mysql://localhost:3306/database name? useunicode=true&characterencoding=utf-8", "User name", "Password"); 8SM =ct.createstatement ();9String selectstring = "Select field from table name where field = ' property value '"Ten sm.executequery (selectstring); One         }   A         Catch(Exception ex) { -System.out.print ("Failure! ");  -         } the          finally{   -              Try{   -                  if(rs!=NULL){   - Rs.close ();  +                  }   -                  if(sm!=NULL){   + Sm.close ();  A                  }   at                  if(ct!=NULL){   - Ct.close ();  -                  }   -                     -              }   -              Catch(Exception ex) { in Ex.printstacktrace ();  -              }   to          }   +     

Java connection to SQL Server:

Import SQLJDBC Driver Package

Here are the driver packs: http://download.csdn.net/download/u011200062/8361465

Next write this:

    

Connection ct =NULL; Statement SM=NULL; ResultSet RS=NULL; Try{class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); String URL= "Jdbc:sqlserver://localhost:1433;databasename= database name"; String User= "SA";//user name and password to log on to SQL ServerString pwd= "123456";//Passwordct=drivermanager.getconnection (URL,USER,PWD); SM=ct.createstatement (); RS= Sm.executequery ("Select field from table name where field = ' property value '"); }           Catch(Exception ex) {}finally{                Try{                    if(rs!=NULL) {rs.close (); }                    if(sm!=NULL) {sm.close (); }                    if(ct!=NULL) {ct.close (); }                                    }                Catch(Exception ex) {ex.printstacktrace (); }            }  

If the Java Web is not connected to SQL Server, you need to open the network protocol, refer to this classmate: http://blog.csdn.net/stewen_001/article/details/19553173/

  

Java Connection Mysql,sql Server database

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.