不知道你要连接什么数据库以下是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 "";
}
}
}
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 "";
}
}
}
解决方案 »
- 字符流中read方法返回值是int,为什么输出是char?
- 如何在已经定义的JPanel绘制曲线
- 拜托各位帮个忙改个程序!!!!!!!!!!!!!!!!!!!!!!谢谢了 十万火急!!!!!!!(1)
- 一个包里的.java文件怎么不能编译了呢?
- 一个同步的小问题?
- 求高手指导一个“Java的功能实现与预期不符的Socket代码编程”的技术问题..
- 悬百分(一个形式而已)请大家来谈谈Java中抽象类和接口的区别?
- 想获取一个网页,但是因为使用的是代理服务器,我怎样在程序里设置代理服务器?
- 菜鸟问题:为什么我的这些代码通过了编译却运行不了?
- java和oracle字符串长度问题
- [请教]我们的设计思路,希望各位高手给予指点,谢谢!
- 写了一个文本编辑器,如何实现查找替换功能?
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驱动
SQLSERVER:msbase.jar,mssqlserver.jar,msutil.jar;
ORACLE: classes111.zip或classes12.zip
* <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();
}
}
}