首页 > 数据库技术 > 详细

jdbc连接数据库

时间:2014-11-07 20:37:57      阅读:252      评论:0      收藏:0      [点我收藏+]
 1 package com.howe2;
 2 import java.sql.*;
 3 
 4 public class test2 {
 5     public static void main(String [] args)
 6     {
 7         PreparedStatement ps = null;
 8         Connection con = null;
 9         try
10         {
11             //1. 加载驱动
12             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
13             //2. 获得连接
14             con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName = howe", "sa", "sa");
15             //3. 创建PreparedStatement
16             ps = con.prepareStatement("insert into teacher values(?,?,?)");//一般用? 表示
17             //给?赋值
18             ps.setString(1, "t010");
19             ps.setString(2, "wek");
20             ps.setString(3, "Shanghai");
21             //执行
22             int i = ps.executeUpdate();
23             if(i == 1)
24             {
25                 System.out.println("insert OK");
26             }
27             else
28                 System.out.println("not OK");
29         }catch(Exception e)
30         {
31             e.printStackTrace();
32         }
33         finally
34         {
35             try {
36                 //关闭资源
37                 if(ps != null)
38                     ps.close();
39                 if(con != null)
40                     con.close();
41             } catch (SQLException e) {
42                 // TODO Auto-generated catch block
43                 e.printStackTrace();
44             }
45         }
46     }
47 }

 

jdbc连接数据库

原文:http://www.cnblogs.com/Howe-Young/p/4082205.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!