J2SE学习笔记(5)JDBC

JDBC:Java DataBase Connectivity(Java 数据库连接技术),它是将Java与SQL结合且独立于特定的数据库系统的应用程序编程接口(API--它是一种可用于执行SQL语句的Java API,即由一组用Java语言编写的类与接口所组成)。

 

public   class  FirstConnection  {

    
public static void main(String[] args) {
        Connection conn 
= null;
        Statement stmt 
= null;
        ResultSet rs  
= null;
        
try {
            
// 加载驱动程序 (默认自动注册)
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            
// 注册驱动程序
            /*
             * Driver driver = new SQLServerDriver();
             * DriverManager.registerDriver(driver);
             
*/


            String url 
= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind";
            String username 
= "sa";
            String password 
= "sa";
            
            
// 获取连接
            conn = DriverManager.getConnection(url, username, password);
            System.out.println(conn);
            
// 创建Statement
            stmt = conn.createStatement();
            String sql 
= "insert into Categories (CategoryName,Description,Picture ) values ('电脑','test','1.jpg')";
            
// 执行语句
            
//更新操作
            /*int count = stmt.executeUpdate(sql);
            System.out.println("受影响的纪录条数:" + count);
*/

            
//查询操作
            String querySql = "select * from Categories";
            
//获取查询结果
            rs = stmt.executeQuery(querySql);
            
while(rs.next()){
                System.out.print(rs.getString(
1));
                System.out.print(rs.getString(
"CategoryName"));
                System.out.println(rs.getString(
3));
            }


        }
 catch (ClassNotFoundException e) {
            e.printStackTrace();
            
// System.out.println("类驱动程序无法加载:"+e.getMessage());
        }
 catch (SQLException e) {

            e.printStackTrace();
        }
 finally {
            
//关闭rs
            if (rs != null)
                
try {
                    rs.close();

                }
 catch (SQLException e1) {

                    System.out.println(e1);
                }

            
//关闭stmt
            if (stmt != null)
                
try {
                    stmt.close();

                }
 catch (SQLException e1) {

                    System.out.println(e1);
                }

            
//关闭连接conn    
            if (conn != null)
                
try {
                    conn.close();
                }
 catch (SQLException e) {
                    e.printStackTrace();
                }


        }


    }


}

 

public   class  PreparedDemo  {

    
public static void main(String[] args) {
        Connection conn 
= null;
        PreparedStatement ps 
= null;
        
try {
            
// 加载驱动程序 (默认自动注册)
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            
// 注册驱动程序
            /*
             * Driver driver = new SQLServerDriver();
             * DriverManager.registerDriver(driver);
             
*/


            String url 
= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind";
            String username 
= "sa";
            String password 
= "sa";
            
            
// 获取连接
            conn = DriverManager.getConnection(url, username, password);
            System.out.println(conn);
            
// 创建prepareStatement
            String sql = "insert into Categories (CategoryName,Description ) values (?,?)";        
            ps 
= conn.prepareStatement(sql);
            
// 给参数负值
            ps.setString(1,"computer");
            ps.setString(
2,"computer vvv");
            
            
//执行语句
            ps.executeUpdate();
            
        }
 catch (ClassNotFoundException e) {
            e.printStackTrace();
            
// System.out.println("类驱动程序无法加载:"+e.getMessage());
        }
 catch (SQLException e) {

            e.printStackTrace();
        }
 finally {
            
            
//关闭ps
            if (ps != null)
                
try {
                    ps.close();

                }
 catch (SQLException e1) {

                    System.out.println(e1);
                }

            
//关闭连接conn    
            if (conn != null)
                
try {
                    conn.close();
                }
 catch (SQLException e) {
                    e.printStackTrace();
                }


        }


    }


}

你可能感兴趣的:(java,jdbc,Microsoft,J2SE,null,sqlserver)