标题:求救! java和数据库如何连接
取消只看楼主
zhlo110
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-3-13
 问题点数:0 回复次数:0 
求救! java和数据库如何连接

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

class TestDB { public static void main (String args[]) { try { Connection conn = getConnection(); Statement stat = conn.createStatement();

stat.execute("CREATE TABLE Greetings (Name CHAR(20))"); stat.execute( "INSERT INTO Greetings VALUES ('Hello, DataBase!')");

ResultSet result = stat.executeQuery("SELECT * FROM Greetings"); while (result.next()) System.out.println(result.getString(1)); result.close();

stat.execute("DROP TABLE Greetings"); stat.close(); conn.close(); } catch (SQLException ex) { while (ex != null) { ex.printStackTrace(); ex = ex.getNextException(); } } catch (IOException ex) { ex.printStackTrace(); } }

/* Gets a connection from the properties specified in the file database.properties */ 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"); // return DriverManager.getConnection(url, props); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password");

return DriverManager.getConnection(url, username, password); } } 一道java和数据库的连接代码! 编译可以完成,但运行时抛出 SQLException异常,no suiable driver 为什么????????????????????? 请高手指点,

搜索更多相关主题的帖子: java 数据库 stat import 
2005-03-13 17:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-13270-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.266130 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved