在JAVA里怎样与数据库联系 我在编写学生管理系统的时候有许多已有学生资料做成了数据库,现在想与数据库取得联系请问下各位高手们帮下我这个菜鸟! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用的什么数据库如果是MySQL的话,下面提供一端代码:db.properties文件内容: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=xxxx jdbc.password=xxxxxx 接着在数据库操作类中使用如下语句进行调用即可: public class DBConnection { public Connection getConnection() throws Exception { InputStream is = this.getClass().getClassLoader().getResourceAsStream("database.properties"); Properties property = new Properties(); property.load(is); String driverClassName = property.getProperty("jdbc.driverClassName"); String url = property.getProperty("jdbc.url"); String username = property.getProperty("jdbc.username"); String password = property.getProperty("jdbc.password"); Connection conn = null; try { Class.forName(driverClassName); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; 自己放入连接的驱动包啊,其它的数据库大致都一样,该下properties文件就差不多可以了 import java.sql.*如果你用的是 MySQL数据库 首先Class.forName(com.mysql.jdbc.Driver);String url="jdbc:mysql://localhost/yourdatabasename?user=root&password=root";Connection conn = Drivermanager.getConnection(url);这就是建立连接了给你个DBase类这就是 封装了数据库的链接 关闭的类import java.sql.*;public class DBase { public static Connection getConn(){ Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root"); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } return conn ; } public static Statement getStmt(Connection conn){ Statement stmt = null ; try{ stmt = conn.createStatement(); }catch(SQLException e){ e.printStackTrace(); } return stmt ; } public static ResultSet executeQuery(Statement stmt,String sql){ ResultSet rs = null ; try{ rs = stmt.executeQuery(sql); }catch(SQLException e ){ e.printStackTrace(); } return rs ; } public void close(Connection conn){ if(conn!= null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } conn = null ; } } public void close(Statement stmt){ if(stmt!= null){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } stmt = null ; } } public void close(ResultSet rs){ if(rs!= null){ try{ rs.close(); }catch(SQLException e){ e.printStackTrace(); } rs = null ; } }} 我用的 SQL2005 应该怎么做啊 /* * s2jsp.lg.dao.impl.BaseDao.java * 2007-7-18 * Dao的基类,使用JDBC连接数据库、释放资源、执行sql,可以被其他Dao实现类继承或实例化使用 */package s2jsp.lg.dao.impl;import java.sql.*;public class BaseDao { public final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库驱动 public final static String URL = "jdbc:sqlserver://localhost;DataBaseName=bbs"; // url public final static String DBNAME = "sa"; // 数据库用户名 public final static String DBPASS = "sa"; // 数据库密码 /** * 得到数据库连接 * @throws ClassNotFoundException * @throws SQLException * @return 数据库连接 */ public Connection getConn() throws ClassNotFoundException, SQLException{ Class.forName(DRIVER); //注册驱动 Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS); //获得数据库连接 return conn ; //返回连接 } /** * 释放资源 * @param conn 数据库连接 * @param pstmt PreparedStatement对象 * @param rs 结果集 */ public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) { /* 如果rs不空,关闭rs */ if(rs != null){ try { rs.close();} catch (SQLException e) {e.printStackTrace();} } /* 如果pstmt不空,关闭pstmt */ if(pstmt != null){ try { pstmt.close();} catch (SQLException e) {e.printStackTrace();} } /* 如果conn不空,关闭conn */ if(conn != null){ try { conn.close();} catch (SQLException e) {e.printStackTrace();} } } /** * 执行SQL语句,可以进行增、删、改的操作,不能执行查询 * @param sql 预编译的 SQL 语句 * @param param 预编译的 SQL 语句中的‘?’参数的字符串数组 * @return 影响的条数 */ public int executeSQL(String preparedSql,String[] param) { Connection conn = null; PreparedStatement pstmt = null; int num = 0; /* 处理SQL,执行SQL */ try { conn = getConn(); // 得到数据库连接 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象 if( param != null ) { for( int i = 0; i < param.length; i++ ) { pstmt.setString(i+1, param[i]); // 为预编译sql设置参数 } } num = pstmt.executeUpdate(); // 执行SQL语句 } catch (ClassNotFoundException e) { e.printStackTrace(); // 处理ClassNotFoundException异常 } catch (SQLException e) { e.printStackTrace(); // 处理SQLException异常 } finally { closeAll(conn,pstmt,null); // 释放资源 } return num; }} 这个是代码是sql2005的JDBC连接代码.连接2005可能出现的问题:SQL2005连接可能出现连接失败的问题.是由于2005安装完成默认是关闭网络连接的.你可以打开2005的网络连接就可以了2005的驱动要自己去MS的网站下载.驱动错误不会提示他本身错误.只会提示数据库有问题.在项目里没有导入数据库驱动.使你代码没有错误,驱动也对了,网络也开了,还是会异常.一定不要忘记引入驱动包到项目里我的代码里的用户名和密码可能不适合你的数据库.记得修改下. 楼主,建议你去找本有hibernate的书看看,仔细看一个星期就基本能用了,反正学java的人迟早要用的 说来话长。谷歌一下JAVA连接数据库 数组的计数 进度条无法更新,只在完成的时候更新一次 java 如何实现开机自动运行 how to write Java Internationalized program 求输出word文档的第三方包 想不出來!! 请各位指点下以下程序的输出问题。。 一个很简单的关于int和float的问题 关于线程池的问题 在线谈谈:classpath 再讨论(简直是恶梦!!!) Java初学dos配置 实现两个不同的接口时,两个接口时存在同名的方法,如何处理。
如果是MySQL的话,下面提供一端代码:db.properties文件内容:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=xxxx
jdbc.password=xxxxxx 接着在数据库操作类中使用如下语句进行调用即可:
public class DBConnection {
public Connection getConnection() throws Exception {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("database.properties");
Properties property = new Properties();
property.load(is);
String driverClassName = property.getProperty("jdbc.driverClassName");
String url = property.getProperty("jdbc.url");
String username = property.getProperty("jdbc.username");
String password = property.getProperty("jdbc.password");
Connection conn = null;
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn; 自己放入连接的驱动包啊,
其它的数据库大致都一样,该下properties文件就差不多可以了
如果你用的是 MySQL数据库 首先
Class.forName(com.mysql.jdbc.Driver);
String url="jdbc:mysql://localhost/yourdatabasename?user=root&password=root";
Connection conn = Drivermanager.getConnection(url);
这就是建立连接了
给你个DBase类这就是 封装了数据库的链接 关闭的类import java.sql.*;public class DBase {
public static Connection getConn(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn ;
}
public static Statement getStmt(Connection conn){
Statement stmt = null ;
try{
stmt = conn.createStatement();
}catch(SQLException e){
e.printStackTrace();
}
return stmt ;
}
public static ResultSet executeQuery(Statement stmt,String sql){
ResultSet rs = null ;
try{
rs = stmt.executeQuery(sql);
}catch(SQLException e ){
e.printStackTrace();
}
return rs ;
}
public void close(Connection conn){
if(conn!= null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
conn = null ;
}
}
public void close(Statement stmt){
if(stmt!= null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
stmt = null ;
}
}
public void close(ResultSet rs){
if(rs!= null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
rs = null ;
}
}
}
* s2jsp.lg.dao.impl.BaseDao.java
* 2007-7-18
* Dao的基类,使用JDBC连接数据库、释放资源、执行sql,可以被其他Dao实现类继承或实例化使用
*/
package s2jsp.lg.dao.impl;import java.sql.*;public class BaseDao {
public final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库驱动
public final static String URL = "jdbc:sqlserver://localhost;DataBaseName=bbs"; // url
public final static String DBNAME = "sa"; // 数据库用户名
public final static String DBPASS = "sa"; // 数据库密码
/**
* 得到数据库连接
* @throws ClassNotFoundException
* @throws SQLException
* @return 数据库连接
*/
public Connection getConn() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER); //注册驱动
Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS); //获得数据库连接
return conn ; //返回连接
}
/**
* 释放资源
* @param conn 数据库连接
* @param pstmt PreparedStatement对象
* @param rs 结果集
*/
public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {
/* 如果rs不空,关闭rs */
if(rs != null){
try { rs.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果pstmt不空,关闭pstmt */
if(pstmt != null){
try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果conn不空,关闭conn */
if(conn != null){
try { conn.close();} catch (SQLException e) {e.printStackTrace();}
}
}
/**
* 执行SQL语句,可以进行增、删、改的操作,不能执行查询
* @param sql 预编译的 SQL 语句
* @param param 预编译的 SQL 语句中的‘?’参数的字符串数组
* @return 影响的条数
*/
public int executeSQL(String preparedSql,String[] param) {
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
/* 处理SQL,执行SQL */
try {
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
if( param != null ) {
for( int i = 0; i < param.length; i++ ) {
pstmt.setString(i+1, param[i]); // 为预编译sql设置参数
}
}
num = pstmt.executeUpdate(); // 执行SQL语句
} catch (ClassNotFoundException e) {
e.printStackTrace(); // 处理ClassNotFoundException异常
} catch (SQLException e) {
e.printStackTrace(); // 处理SQLException异常
} finally {
closeAll(conn,pstmt,null); // 释放资源
}
return num;
}}
SQL2005连接可能出现连接失败的问题.是由于2005安装完成默认是关闭网络连接的.你可以打开2005的网络连接就可以了2005的驱动要自己去MS的网站下载.驱动错误不会提示他本身错误.只会提示数据库有问题.在项目里没有导入数据库驱动.使你代码没有错误,驱动也对了,网络也开了,还是会异常.一定不要忘记引入驱动包到项目里我的代码里的用户名和密码可能不适合你的数据库.记得修改下.