Java JDBC连接的一个例子
2008-01-05 09:04:26 来源:WEB开发网核心提示:import java.lang.*;import java.util.*;import java.sql.*;public class cn1{ public static void main(String args[]) { String s="顾强"; String s
import java.lang.*;
import java.util.*;
import java.sql.*;
public class cn1
{
public static void main(String args[])
{
String s="顾强";
String s1="ab";
String drv=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
int rowcount=0;
PRoperties p = new Properties();
try
{
//注册数据库驱动程序为Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (java.lang.ClassNotFoundException e)
{
//这样写是为了方便调试程序,出错打印mydb()就知道在什么地方出错了
System.err.println("mydb(): " + e.getMessage());
}
try
{
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orc8i", "java", "88888888");
}
catch (SQLException ex)
{
System.err.println("conn:"+ex.getMessage());
}
if (conn!=null)
System.out.println("connection sUCcessful");
else
System.out.println("connection failure");
try
{
stmt=conn.createStatement();
}
catch (SQLException ex)
{
System.err.println("createStatement();"+ex.getMessage());
}
try
{
rs=stmt.executeQuery("select * from userdata");
}
catch (SQLException ex)
{
System.err.println("stmt.excuteQuery();"+ex.getMessage());
}
try
{
while (rs.next())
{
System.out.println("username: "+ rs.getString(1));
System.out.println("passWord: "+rs.getString(2));
System.out.println("email: " +rs.getString(3));
}
}
catch (SQLException ex)
{
System.err.println("A ERROR is failure" +ex.getMessage());
更多精彩
赞助商链接