当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

Java JDBC连接的一个例子

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());

}


//test1();
p=System.getProperties();

//p.list(System.out);
drv=p.getProperty("java.class.path");
//System.out.println("driver="+drv);



}


public static void test1()
{
cn2 cn2obj = new cn2();
printClassName(cn2obj);
}

public static void printClassName(Object obj)
{
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}


}

class cn2
{

public void test()
{
System.out.println("11111111111");

}

public String toString()
{
return "1111";
}

}


相关内容
赞助商链接