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