Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:【求助】关于jdbc驱动设置的问题。 [Re:jameszhang]
caoyi





发贴: 32
积分: 0
于 2004-04-29 22:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jameszhang wrote:
java.sql.SQLException: No suitable driver
驱动程序与数据库不匹配

当时鹅用ORACLE8的JDBC 访问9I时出过这种错!


我用jc编译和运行都正常。该程序就是corejava2中的例子。我在jc中是把驱动设置为archive。驱动放在j2sdk1.4.2_04\jre\lib\ext下。

import java.io.*;
import java.util.*;
import java.sql.*;

class ExecSQL
{
public static void main(String args[])
{
try
{
Reader reader;
if(args.length==0)
reader = new InputStreamReader(System.in);
else
reader = new FileReader(args[0]);
Connection conn = getConnection();
Statement stat = conn.createStatement();

BufferedReader in = new BufferedReader(reader);

boolean done = false;
while(!done)
{
if(args.length == 0)
System.out.println(
"Enter command or a blank line to exit:");

String line = in.readLine();
if(line == null||line.length() == 0)
done = true;
else
{
try
{
boolean hasResultSet = stat.execute(line);
if(hasResultSet)
showResultSet(stat);
}
catch(SQLException ex)
{
while(ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
}
}
in.close();
stat.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException, IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();

String drivers = props.getProperty("jdbc.drivers");
if(drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

return
DriverManager.getConnection(url, username, password);
}
public static void showResultSet(Statement stat) throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount();

for(int i = 1;i <= columnCount; i++)
{
if(i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println();

while(result.next())
{
for(int i = 1; i <= columnCount; i++)
{
if(i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}

文件database.properties
jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=blue
jdbc.password=blue




话题树型展开
人气 标题 作者 字数 发贴时间
4594 【求助】关于jdbc驱动设置的问题。 caoyi 412 2004-04-29 21:30
3059 Re:【求助】关于jdbc驱动设置的问题。 jameszhang 86 2004-04-29 21:51
3851 Re:【求助】关于jdbc驱动设置的问题。 caoyi 2956 2004-04-29 22:28

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923