下面的代码显示了连接SQL Server数据库时,
连接数据库的步骤:
1、注册驱动 (只做一次)
建立连接的方法:
下面分别给出插入、修改、删除、查询的基本方式的代码:
修改数据:
删除数据:
查询数据:
转自:http://blog.csdn.net/yanghai0321/article/details/7288363
连接数据库的步骤:
1、注册驱动 (只做一次)
2、建立连接(Connection)
3、创建执行SQL的语句(Statement)
4、执行语句
5、处理执行结果(ResultSet)
6、释放资源
建立连接的方法:
01 | Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" ); |
02 | //加载数据库引擎,返回给定字符串名的类 |
03 | String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test" ; |
04 | //test为你的数据库的名称 |
05 | String user= "用户名" ; |
06 | String password= "密码" ; |
07 |
08 | Connection conn=DriverManager.getConnection(url,user,password); |
09 | //连接数据库对象 |
10 | Statement stmt=conn.createStatement(); |
11 | //创建SQL命令对象 |
12 |
13 |
14 | String query= "" ; //创建表SQL语句 |
15 | stmt.executeUpdate(query); //执行SQL命令对象 |
16 |
17 | String a= "INSERT/DELETE.... INTO/FROM “表名” VALUES(....,...)" ; |
18 | .... |
19 | .... |
20 | .... |
21 | stmt.executeUpdate(a); //执行SQL命令对象 |
22 |
23 | //读取数据 |
24 | ResultSet rs=stmt.executeQuery( "SELECT * FROM “表名”" ); |
25 | //循环输出每一条记录 rs最初指向第一条记录的前面 |
26 | while (rs.next){ |
27 | |
28 | } |
29 |
30 | 最后关闭连接 |
31 | stmt.close(); |
32 | con.close(); |
33 |
34 | 数据库连接(Connection)是非常稀有的资源,用完后必须马上释放, |
35 | 如果Connection不能及时正确的关闭将导致系统宕机。 |
36 | Connection的使用原则是尽量晚创建,尽量早的释放。 |
插入数据:
01 | import java.sql.*; |
02 |
03 | /** |
04 |
* @version 2012-02-22 |
05 |
* @author |
06 |
*/ |
07 | public class InsertDemo { |
08 | public static void main(String[] args) throws SQLException { |
09 | Connection conn = null ; |
10 | Statement stmt = null ; |
11 | String JDriver= "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; //SQL鏁版嵁搴撳紩鎿� |
12 | String connectDB= "jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest" ;//鏁版嵁婧惵� |
13 | String user= "sa" ; |
14 | String pwd= "sql" ; |
15 | try { |
16 | // 动态导入数据库的驱动 |
17 | Class.forName(JDriver); |
18 | // 获取数据库链接 |
19 | conn = DriverManager.getConnection(connectDB,user,pwd); |
20 | // 创造SQL语句 |
21 | String sql = "INSERT INTO JDBCTestTable ( user_name, user_password ) VALUES ( 'balabala', '123456' )" ; |
22 | // 执行SQL语句 |
23 | stmt = conn.createStatement(); |
24 | stmt.executeUpdate(sql); |
25 | System.out.println( "插入数据成功" ); |
26 | } catch (Exception e) { |
27 | e.printStackTrace(); |
28 | stmt.close(); |
29 | conn.close(); |
30 | } |
31 | } |
32 | } |
01 | import java.sql.Connection; |
02 | import java.sql.DriverManager; |
03 | import java.sql.SQLException; |
04 | import java.sql.Statement; |
05 |
06 | public class UpdateDemo { |
07 |
08 | public static void main(String[] args) throws SQLException { |
09 | Connection conn = null ; |
10 | Statement stmt = null ; |
11 | try { |
12 | // 动态导入数据库的驱动 |
13 | Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); |
14 | // 获取数据库链接 |
15 | conn = DriverManager.getConnection( |
16 | "jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest" , |
17 | "sa" , "sql" ); |
18 | // 创造SQL语句 |
19 | String sql = "UPDATE JDBCTestTable SET user_password = '456' WHERE user_name = 'Eric'" ; |
20 | // 执行SQL语句 |
21 | stmt = conn.createStatement(); |
22 | stmt.executeUpdate(sql); |
23 | System.out.println( "更新数据成功" ); |
24 | } catch (Exception e) { |
25 | e.printStackTrace(); |
26 | stmt.close(); |
27 | conn.close(); |
28 | } |
29 | } |
30 | } |
01 | import java.sql.Connection; |
02 | import java.sql.DriverManager; |
03 | import java.sql.Statement; |
04 |
05 | public class DeleteDemo { |
06 |
07 | public static void main(String[] args){ |
08 | String JDriver= "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; |
09 | String url= "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest" ; |
10 | String user= "sa" ; |
11 | String password= "sqlyanghai" ; |
12 | try { |
13 | Class.forName(JDriver); // 动态导入数据库的驱动 |
14 | Connection conn=DriverManager.getConnection(url, user, password); // 获取数据库链接 |
15 | String query= "DELETE FROM JDBCTestTable WHERE user_name='Eric'" ; // 创造SQL语句 |
16 | Statement stmt=conn.createStatement(); // 执行SQL语句 |
17 | stmt.executeUpdate(query); |
18 | System.out.println( "删除数据成功" ); |
19 | stmt.close(); |
20 | conn.close(); |
21 | } catch (Exception e){ |
22 | e.printStackTrace(); |
23 | } |
24 | } |
25 | } |
01 | import java.sql.Connection; |
02 | import java.sql.DriverManager; |
03 | import java.sql.ResultSet; |
04 | import java.sql.Statement; |
05 |
06 | public class QueryDemo { |
07 |
08 | public static void main(String[] args){ |
09 | String JDriver= "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; |
10 | String url= "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest" ; |
11 | String user= "sa" ; |
12 | String password= "sql" ; |
13 | try { |
14 | Class.forName(JDriver); // 动态导入数据库的驱动 |
15 | Connection conn=DriverManager.getConnection(url, user, password); // 获取数据库链接 |
16 | String query= "SELECT * FROM JDBCTestTable" ; // 创造SQL语句 |
17 | Statement stmt=conn.createStatement(); // 执行SQL语句 |
18 | ResultSet rs=stmt.executeQuery(query); |
19 | while (rs.next()){ |
20 | System.out.println(rs.getString( "user_name" )+ ":" +rs.getString( 2 )); |
21 | //密码字段的编号从1开始,密码排第二位 |
22 | } |
23 | System.out.println( "查询数据成功" ); |
24 | rs.close(); |
25 | stmt.close(); |
26 | conn.close(); |
27 | } catch (Exception e){ |
28 | e.printStackTrace(); |
29 | } |
30 | } |
31 | } |