?? database.java
字號:
package grad.util;/** * <p>Title:數據庫連接專用包 </p> * @version 1.0 */import java.sql.*;public class DataBase { public Connection conn; public DataBase() { this.connect(); } public boolean connect(){ /* try{// 連接mysql數據庫 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/teachonline?user=wxy&useUnicode=true&characterEncoding=8859_1"; conn=DriverManager.getConnection(url); stmt = conn.createStatement (); }catch(Exception ee){ System.out.println("connect db error:"+ee.getMessage()); return false; } */ /*//利用tomcat連接池配置,連接sql server數據庫 try{ ctx = new InitialContext(); if (ctx == null) throw new Exception("沒有匹配的上下文環境"); ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SqlServermidp"); if (ds == null) throw new Exception("沒有匹配的數據庫"); conn = ds.getConnection(); stmt=conn.createStatement(); }catch(Exception ee){ System.out.println("connect db error:"+ee.getMessage()); return false; } */ try{//用jdbc直接連接sqlserver數據庫 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1305;DatabaseName=teachonline"; String user="sa"; String password=""; conn= DriverManager.getConnection(url,user,password); }catch(Exception ee){ System.out.println("connect db error:"+ee.getMessage()); return false; } return true; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -