對於JDBC資料庫的初始化操作

lonecloud發表於2016-03-20
 1 package com.bluesky.connection;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 public class connection {
 8         private static final String URL="jdbc:mysql://localhost/test";
 9         private static final String USER="root";
10         private static final String PASSWORD="1234";
11         private static Connection conn=null;
12         static{
13 
14             try {
15                 //1.載入驅動程式
16                 Class.forName("com.mysql.jdbc.Driver");
17                 //2.獲取資料庫連線
18                 conn=DriverManager.getConnection(URL, USER, PASSWORD);
19             } catch (ClassNotFoundException e) {
20                 // TODO Auto-generated catch block
21                 e.printStackTrace();
22             } catch (SQLException e) {
23                 // TODO Auto-generated catch block
24                 e.printStackTrace();
25             }
26         }
27         public static Connection getConnnection(){
28             return conn;
29         }
30 //        public static void main(String[] args) throws Exception {
31 //
32 //            //3.運算元據庫
33 //            Statement stmt=conn.createStatement();
34 //            ResultSet rs=stmt.executeQuery("Select * from user");
35 //            while (rs.next()) {
36 //                System.out.println(rs.getString("id")+rs.getString("password"));
37 //            }
38 //        }
39 }

 

相關文章