java连接sql server数据库

1、新建项目,导入包  sqljdbc4.jar或sqljdbc.jar(jdk1.7版本)

2、新建类文件ConnectionDB.java

  1 package hello;

  2 

  3 import java.sql.*;

  4 

  5 /*

  6  * 连接数据库类

  7  */

  8 public class ConnectionDB {

  9 

 10     public ConnectionDB() {

 11 

 12     }

 13 

 14     // 建立与驱动包的联系

 15     private static final String Drive = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

 16 

 17     // 数据库地址

 18     private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=English";

 19 

 20     // 用户名

 21     private final String USER = "sa";

 22 

 23     // 密码

 24     private final String PASSWORD = "940523";

 25 

 26     Connection conn = null; // 定义数据库连接对象

 27 

 28     Statement stmt = null; // 定义SQL命令集对象

 29 

 30     ResultSet rs = null; // 定义结果集对象

 31 

 32     /**

 33      * 连接数据库

 34      * 

 35      */

 36     private Connection getConnection() {

 37         try {

 38             // 加载驱动

 39             Class.forName(Drive);

 40             // 连接数据库

 41             conn = DriverManager.getConnection(URL, USER, PASSWORD);

 42             return conn;

 43         } catch (Exception e) {

 44             e.printStackTrace();

 45             System.out.println("数据库连接失败!");

 46         }

 47         return null;

 48     }

 49 

 50     /**

 51      * 数据库查询操作

 52      * 

 53      */

 54     public void getDateByQuery(String sql) {

 55         try {

 56             // 获取盛装SQL语句的载体

 57             stmt = this.getConnection().createStatement();

 58             // 获取结果集rs

 59             rs = stmt.executeQuery(sql);

 60             // 输出查询结果

 61             while (rs.next())

 62                 System.out.println(rs.getInt(1) + "   " + rs.getString(2)

 63                         + "  " + rs.getString(3));

 64         } catch (Exception e) {

 65             e.printStackTrace();

 66             System.out.println("数据库查询出错!");

 67         }

 68     }

 69 

 70     /**

 71      * 数据库更新操作

 72      * 

 73      */

 74     public void getDataByUpdate(String sql) {

 75         try {

 76             // 获取盛装SQL语句的载体

 77             stmt = this.getConnection().createStatement();

 78             // 获得更新记录条数

 79             int line = stmt.executeUpdate(sql);

 80             System.out.println("更新了" + line + "条记录\n");

 81             // 查询数据库

 82             String sqlQuery = "select *from word";

 83             rs = stmt.executeQuery(sqlQuery);

 84             while (rs.next())

 85                 System.out.println(rs.getInt(1) + "   " + rs.getString(2)

 86                         + "  " + rs.getString(3));

 87         } catch (Exception e) {

 88             e.printStackTrace();

 89             System.out.println("数据库更新出错!");

 90         }

 91 

 92     }

 93 

 94     /**

 95      * 数据库插入操作

 96      * 

 97      */

 98     public void getDataByInsert(String sql) {

 99         try {

100             stmt = this.getConnection().createStatement();

101             int line = stmt.executeUpdate(sql);

102             System.out.println("插入了" + line + "条记录\n");

103             String sqlQuery = "select *from word";

104             rs = stmt.executeQuery(sqlQuery);

105             while (rs.next())

106                 System.out.println(rs.getInt(1) + "   " + rs.getString(2)

107                         + "  " + rs.getString(3));

108         } catch (Exception e) {

109             e.printStackTrace();

110             System.out.println("数据库插入出错!");

111         }

112 

113     }

114 

115     /**

116      * 数据库删除操作

117      */

118     public void getDataByDelete(String sql) {

119         try {

120             stmt = this.getConnection().createStatement();

121             int line = stmt.executeUpdate(sql);

122             System.out.println("删除了" + line + "条记录\n");

123             String sqlQuery = "select *from word";

124             rs = stmt.executeQuery(sqlQuery);

125             while (rs.next())

126                 System.out.println(rs.getInt(1) + "   " + rs.getString(2)

127                         + "  " + rs.getString(3));

128         } catch (Exception e) {

129             e.printStackTrace();

130             System.out.println("数据库删除出错!");

131         }

132 

133     }

134 

135     /**

136      * 关闭数据库连接

137      */

138     public void close() {

139         try {

140             if (rs != null) {

141                 rs.close(); // 关闭结果集

142             }

143 

144             if (stmt != null) {

145                 stmt.close();// 关闭盛装SQL语句的载体

146             }

147 

148             if (conn != null) {

149                 conn.close();// 关闭数据库连接

150             }

151         } catch (SQLException e) {

152             e.printStackTrace();

153             System.out.println("关闭连接出错!");

154             System.exit(0);

155         }

156     }

157 

158 }

简版:

 1 import java.sql.Connection;

 2 import java.sql.DriverManager;

 3 import java.sql.ResultSet;

 4 import java.sql.Statement;

 5 

 6 public class Test {

 7 

 8     public static void main(String[] args) {

 9         try {

10             // 1.注册驱动

11             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

12             // 2.建立连接

13             // 2.1连接串

14             String conStr = "jdbc:sqlserver://localhost:1433;DatabaseName=JavaDB";

15 

16             Connection con = DriverManager.getConnection(conStr, "sa",

17                     "123456");

18             // 创建会话

19             Statement st = con.createStatement();

20 //            String sql = "select count(*) as cnt from T_user where username='"

21 //                    + username + "' and password='" + password + "'";

22             String sql = "select count(*) as cnt from T_user";

23             // 结果集

24             ResultSet rs = st.executeQuery(sql);

25             rs.next();

26 //            if (rs.getInt("cnt") > 0) {

27 //                System.out.println("登录成功!");

28 //            } else {

29 //                System.out.println("登录失败!");

30 //            }

31         } catch (Exception ex) {

32             ex.printStackTrace();

33         }

34     }

35 

36 }

3、使用方法:

 1 public class Main {

 2 

 3     public static void main(String[] args) {

 4         ConnectionDB db = new ConnectionDB();

 5         String QuerySql = "select * from word where id=1";

 6         db.getDateByQuery(QuerySql);

 7 //        String insertSql="insert into word values('134','boy','男孩')";

 8 //        db.getDataByInsert(insertSql);

 9         

10     }

11 

12 }

 

你可能感兴趣的:(SQL Server)