java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
packagecom.upc;importjava.sql.*;importjava.util.*;publicclasslianjie{StringDriver="su...
package com.upc;import java.sql.*;import java.util.*;public class lianjie { String Driver="sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:/daohanggongcheng/daohang/WebRoot/数据库.mdb"; //String url="jdbc:odbc:mydb"; String username=null; String password=null; public static void main(String[] args) { // TODO Auto-generated method stub lianjie jdbc = new lianjie(); } public lianjie(){ try { Class.forName(Driver).newInstance();// 1. 注册驱动 Connection con = DriverManager.getConnection(url,username,password);// 2. 获得与数据库的连接 String sql="SELECT * FROM users where age=15"; Statement stmt=con.createStatement(); ResultSet result=stmt.executeQuery(sql); while(result.next()){//行 String s1=result.getString(1);//列 // s2=result.getString(3); //list1.add(s1); //list2.add(s2); } }catch(Exception ex){ System.out.print(ex); } } }求大神指点哪里有错
展开
展开全部
你的代码好乱,我拷贝下来看过,你写的有点非主流啊!!!
看看我的
package com.jdbc1;
import java.sql.*;
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsoft:sqlsever://127.0.0.1:1433;databaseName=huoying","sa","sa");
ps=ct.prepareStatement("select * from huoying");
rs=ps.executeQuery();
while(rs.next())
{
String name=rs.getString(2);
System.out.println(name);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
看看我的
package com.jdbc1;
import java.sql.*;
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsoft:sqlsever://127.0.0.1:1433;databaseName=huoying","sa","sa");
ps=ct.prepareStatement("select * from huoying");
rs=ps.executeQuery();
while(rs.next())
{
String name=rs.getString(2);
System.out.println(name);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
追问
这是不是access数据库连接吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询