具体怎么连接,还有怎么做Java前台
分数好说
具体怎么连接,还有怎么做Java前台
分数好说
具体的程序我就不写了,给你说下思路吧
如果调用的不是很多的话,可以写个类,定义几个私有变量,URL,USERNAME,PASSWORD,最好是把这几个变量的值放到一个培植文件里,这样修改起来比较方便,然后定义一个PUBLIC方法,在方法里获得CONNECTION,并且RETURN 这个CONNECTION ,写个方法关闭连接,就哦了
import java.lang.String; import java.sql.*; import java.io.*;
//database access object class DbDao { private static DbDao oper = null; private Connection conn = null; private String driver = null; private String url = null; private String username = null; private String password = null; //private constructor, singlton model private DbDao() { //do noting } //private constructor, with params passed in private DbDao(String driver, String url, String username, String password) throws Exception { this.driver = driver; this.url = url; this.username = username; this.password = password; } //set driver public void setDriver(String driver) { this.driver = driver; } //set url public void setUrl(String url) { this.url = url; } //set username public void setUsername(String username) { this.username = username; } //set password public void setPassword(String password) { this.password = password; } //get Driver public String getDriver() { return this.driver; } //get url public String getUrl() { return this.url; } //get username public String getUsername() { return this.username; } //get password public String getpassword() { return this.password; } //get connection to the database public void getConnection() throws Exception { if(conn == null) { Class.forName(this.Driver); conn = DriverManager.getConnection(this.url, this.username, this.password); } } //return an instance of DbDao public static DbDao instance() { if(oper == null) { oper = new DbDao(); } return oper; } //return an instance of DbDao, with parmas passed in public static DbDao instance(String driver, String url, String username, String password) throws Exception { if(oper == null) { oper = new DbDao(driver, url, username, password); } return oper; } //execute query public ResultSet query(String sqlstmt)throws Exception { //connection initialization getConnection(); Statement stmt = this.conn.creatStatement(); return stmt.executeQuery(sqlstmt); } }
//driver public class task10 { private static final String dbDriverName = "oracle.jdbc.driver.OracleDriver"; private static final String dbURL = "jdbc:oracle:thin:@wraith:1521:csci"; private String username; private String password; //get username and password public void public static void main() { DbDao db; try{ db = DbDao.instance(dbDriverName, dbURL, username, password); ResultSet rs = db.query("select * from customer"); while(rs.next()) { System.out.println(rs.getString(1)); } }catch(Exception e) { //error message System.out.println(e.getMessage()); } } }
那个代码只是用来获得connection 连接对象的
要查询数据 还要写
我记得连接sqlServer的时候还需要jar包 至于什么时候用?放在哪儿?就是在你需要读取是据库的时候才用
你可以写一个专门的getdb类 返回的是一个connection对象 到时候直接用connection对象去操作数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 这个是加载驱动的
Connection conn= DriverManager.getConnection(url,user,password);建立连接 获取connection对象