2014年复试数据库上机之数据库连接

 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 
 7 package scut.film;
 8 
 9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 ;
15 
16 /**
17  *
18  * @author Administrator
19  */
20 public class SCUTFILM {
21 
22     /**
23      * @param args the command line arguments
24      */
25     public static void main(String[] args) {
26         // TODO code application logic here
27         String data =  "jdbc:sqlserver://localhost:1433;databaseName=SCUT_FILM";
28         try(
29             Connection conn = DriverManager.getConnection(data ,"qjy","qjy");
30             Statement st = conn.createStatement()){
31             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
32             ResultSet rec = st.executeQuery(
33             //"select *"+"SQL语句" 
34             "select * from 放映厅表"
35             );
36             while(rec.next()){
37                 System.out.println( rec.getString(1)+"   "+rec.getString(2)+"   "+rec.getString(3));
38         }
39             st.close();
40             
41         }catch(SQLException s){
42             System.out.println("SQL error:" + s.toString() + " " +s.getErrorCode() + " " + s.getSQLState());
43         }catch(Exception e){
44             System.out.println("Error:..." + e.getMessage());
45         }
46     }
47     
48 }
原文地址:https://www.cnblogs.com/1184212881-Ark/p/5325814.html