用jdbc连接数据库时无法找到驱动类怎么办,是什么问题?下面是我的代码
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Two {
public static void main(String[] args) {
Two two = new Two();
two.findAll();}
private void findAll() {
Connection con = null;//通过DriverManager.getConnection()获取Statement声明对象 Connection只是接口
Statement stat = null;//通过con.createStatement()获得结果集对象
ResultSet rs = null;//利用Statement对象的executeQuery方法获执行SQL语句,所得结果存入ReasultSet对象内
try {
Class.forName("oracle.jdbc.OracleDriver");//装载驱动类,驱动类通过static块实现在DriverManager中的自动注册
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","yitek");
stat = con.createStatement();
rs = stat.executeQuery("select sname,xuehao,jie,sxueyuan,sxi,sbanji from student");
while (rs.next()) {
System.out.println(rs.getInt("xuehao") + "," + rs.getString("sname") + "," + rs.getDate("jie"));}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);//驱动类无法找到
}catch (SQLException e){
throw new RuntimeException(e);//数据库访问异常
}finally{
try {
if (rs != null) {
rs.close();}
if (stat != null) {
stat.close();}
if (con !=null) {
con.close();}
} catch (SQLException e) {}}}} 展开
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Two {
public static void main(String[] args) {
Two two = new Two();
two.findAll();}
private void findAll() {
Connection con = null;//通过DriverManager.getConnection()获取Statement声明对象 Connection只是接口
Statement stat = null;//通过con.createStatement()获得结果集对象
ResultSet rs = null;//利用Statement对象的executeQuery方法获执行SQL语句,所得结果存入ReasultSet对象内
try {
Class.forName("oracle.jdbc.OracleDriver");//装载驱动类,驱动类通过static块实现在DriverManager中的自动注册
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","yitek");
stat = con.createStatement();
rs = stat.executeQuery("select sname,xuehao,jie,sxueyuan,sxi,sbanji from student");
while (rs.next()) {
System.out.println(rs.getInt("xuehao") + "," + rs.getString("sname") + "," + rs.getDate("jie"));}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);//驱动类无法找到
}catch (SQLException e){
throw new RuntimeException(e);//数据库访问异常
}finally{
try {
if (rs != null) {
rs.close();}
if (stat != null) {
stat.close();}
if (con !=null) {
con.close();}
} catch (SQLException e) {}}}} 展开
2个回答
展开全部
jdbc和连接池对于你这个场景来说,都足够,既然用spring管理了,建议还是使用连接池,另外,spring自身没有实现连接池,一般都是对第三方连接池的包装,常见的有C3P0,dbcp以及最近比较流行的boneCP等,这几个配置都差不多太多,以boneCP为例:
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="idleMaxAge" value="240" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="2" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="idleMaxAge" value="240" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="2" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询