java连接sql server数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package code;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;
public class DbAdapter { Connection con; Statement sm; ResultSet rs; PreparedStatement ps=null; public static void main(String[] args) { DbAdapter s = new DbAdapter(); String sql = "update users set username='1' where username='1'"; s.LinkSql(sql); } public boolean LinkSql(String sql) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=Student","sa","327523057"); sm = con.createStatement(); rs = sm.executeQuery(sql); if(rs.next()) { return true; } else return false; } catch (Exception e) { e.printStackTrace(); } finally { try { if(sm != null) sm.close(); if(con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } return false; } }
|
需要引入包:
sqljdbc.zip
引入包可以参考:http://blog.okgoes.com/index/index/detail?id=9&type=blog&k=MkTxKNjspe&t=1496288987