package database;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;/** * @author jerrychen * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class DBConnect {
public DBConnect(){
}
public static Connection getConnect() throws SQLException{ //String url = "jdbc:odbc:school"; String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school;User=sa;Password=sa"; Connection con = null; try { //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); con = DriverManager.getConnection(url); } catch (ClassNotFoundException e) { System.err.println(e.getMessage()); } if(con == null){ throw new SQLException("didn't get connection!"); } return con; }}
import java.sql.DriverManager;
import java.sql.SQLException;/**
* @author jerrychen
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class DBConnect {
public DBConnect(){
}
public static Connection getConnect() throws SQLException{
//String url = "jdbc:odbc:school";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school;User=sa;Password=sa";
Connection con = null;
try {
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.err.println(e.getMessage());
}
if(con == null){
throw new SQLException("didn't get connection!");
}
return con;
}}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import Microsoft.DVAP.PSO.PSOConfig;public class JDBCHelper {
private String driverName;
private String dbURL;
private static JDBCHelper helper;
private JDBCHelper() throws RuntimeException
{
try
{
driverName = "org.gjt.mm.mysql.Driver";
dbURL = "jdbc:mysql://MyDbComputerNameOrIP:
3306/myDatabaseName",sUsr, sPwd";
Class.forName(driverName).newInstance();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public Connection getConnection() throws RuntimeException
{
Connection conn = null;
try
{
conn = DriverManager.getConnection(dbURL);
}
catch(SQLException e)
{
throw new RuntimeException(e);
}
return conn;
}
public synchronized static JDBCHelper getDefault()
{
if(helper == null)
{
helper = new JDBCHelper();
}
return helper;
}
public static void main(String[] args) throws Exception {
PSOConfig.setPropertyFile(ConfigInfo.CONFIG_FILE);
JDBCHelper helper = JDBCHelper.getDefault();
Connection conn = helper.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select name from sysobjects");
while(rs.next())
{
System.out.println(rs.getString("name"));
}
conn.close();
}
}