求一个与数据库连接的JAVA源程序。。谢谢了~~

wmillerchina 2004-09-17 10:35:16
一个与数据库连接的JAVA源程序。。谢谢了~~
小弟这里常出问题。想看看各位的程序
...全文
149 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
globaldf01 2004-09-22
  • 打赏
  • 举报
回复
http://java.net.cn/documents/20040903/38153129.html
blueice2002 2004-09-21
  • 打赏
  • 举报
回复
package practise;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

import java.sql.*;

class connectMSSQLSERVER {
public static void main(String[] args) throws SQLException,
ClassNotFoundException {

String dburl =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=text";
String user = "sa";
String password = "password";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection c = DriverManager.getConnection(dburl, user, password);

if (c.isClosed()) {
System.out.println("error");
return;
}

c.setAutoCommit(false);
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM test");

while (r.next()) {
System.out.println(r.getString(1) + r.getString("name"));
}
s.close();
c.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
fucr_364204 2004-09-20
  • 打赏
  • 举报
回复
通过JDBC连接SQLSERVER 和 ORACLE 需要JDBC驱动包,
SQLSERVER:msbase.jar,mssqlserver.jar,msutil.jar;
ORACLE: classes111.zip或classes12.zip
fucr_364204 2004-09-20
  • 打赏
  • 举报
回复
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://192.168.1.2:1433;DatabaseName=fcr";
/****SQLServer****/

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.114:1521:oratest";
//oratest为数据库的SID
/********oracle*****/
String user="sa";
String password="sa";
connection= DriverManager.getConnection(url,user,password);
前提要有JDBC驱动
redlaputa 2004-09-18
  • 打赏
  • 举报
回复
http://www.yesky.com/SoftChannel/72342371945283584/20020423/1608282.shtml
weimenren 2004-09-18
  • 打赏
  • 举报
回复
不知道你要连接什么数据库

以下是Oracle oci连接的一个例子,你可以到

http://download-west.oracle.com/docs/cd/B10501_01/java.920/a96654/getsta.htm#1001200

去看原文,不过可能要求你在oracle上注册

Although JdbcCheckup.java is a simple program, it demonstrates several important functions by executing the following:

imports the necessary Java classes, including JDBC classes
registers the JDBC driver
connects to the database
executes a simple query
outputs the query results to your screen
"First Steps in JDBC", describes these functions in greater detail. A listing of JdbcCheckup.java for the JDBC OCI driver appears below.

/*
* This sample can be used to check the JDBC installation.
* Just run it and provide the connect information. It will select
* "Hello World" from the database.
*/

// You need to import the java.sql package to use JDBC
import java.sql.*;

// We import java.io to be able to read from the command line
import java.io.*;

class JdbcCheckup
{
public static void main(String args[])
throws SQLException, IOException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Prompt the user for connect information
System.out.println("Please enter information to test connection to
the database");
String user;
String password;
String database;

user = readEntry("user: ");
int slash_index = user.indexOf('/');
if (slash_index != -1)
{
password = user.substring(slash_index + 1);
user = user.substring(0, slash_index);
}
else
password = readEntry("password: ");
database = readEntry("database(a TNSNAME entry): ");

System.out.print("Connecting to the database...");
System.out.flush();

System.out.println("Connecting...");
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:@" + database, user, password);
System.out.println("connected.");

// Create a statement
Statement stmt = conn.createStatement();

// Do the SQL "Hello World" thing
ResultSet rset = stmt.executeQuery("select 'Hello World'
from dual");

while (rset.next())
System.out.println(rset.getString(1));
// close the result set, the statement and connect
rset.close();
stmt.close();
conn.close();
System.out.println("Your JDBC installation is correct.");
}

// Utility function to read a line from standard input
static String readEntry(String prompt)
{
try
{
StringBuffer buffer = new StringBuffer();
System.out.print(prompt);
System.out.flush();
int c = System.in.read();
while (c != '\n' && c != -1)
{
buffer.append((char)c);
c = System.in.read();
}
return buffer.toString().trim();
}
catch(IOException e)
{
return "";
}
}
}

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧