对于JDBC数据库的初始化操作
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 }