import java.sql.*;
import java.io.*;
public class L_j {
public static void main(String[] args)throws IOException
{ test();}
public static String test(){
try
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String con="jdbc:microsoft:sqlserver://172.127.10.5:1433;DatabaseName=test";
String user="sa";
String password="";
Class.forName(driver);
System.out.println("驱动sqlserver成功");
Connection conn = DriverManager.getConnection(con,user,password);
System.out.println("连接sqlserver数据库成功");
Statement stmt=conn.createStatement();
String sql="";
Statement ostmt=oraclejdbc();
ResultSet ors=ostmt.executeQuery("select * from xxh_test");
while(ors.next()){
ResultSet rs1=stmt.executeQuery("select * from Student");
while(rs1.next()){
if (ors.getString(1).equals(rs1.getString(1)))
continue;
stmt.executeUpdate("insert into Student values('"+ors.getString(1)+"','"+ors.getString(2)+"','"+ors.getString(3)+"','"+ors.getString(4)+"')");
}
}
sql ="select * from Student";
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
int colcount=rsmd.getColumnCount();
while(rs.next())
{
for(int i=0;i<colcount;i++){
System.out.print(rs.getString(i+1)+"\t");
}System.out.println();
}
ors.close();
ostmt.close();
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
return null;
} /**create a method
*get oracle for jdbc
*connection
*insert . update. delete*/
public static Statement oraclejdbc(){
//Statement ostmt = null;
try
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("找到oracle驱动!");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print(e.getMessage());
}
String url="jdbc:oracle:thin:@web:1521:oradb";
Connection oconn=DriverManager.getConnection(url,"compiere","");
System.out.println("oracle驱动连接成功!");
Statement ostmt=oconn.createStatement();
return ostmt;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println(ex.getSQLState());
}
}return null;
}
}
我现在遇到的难题是从一张表插入到另一张表记录之前我想对其进行判断,如果该记录已有则不添加,可我的功能每次只能添加一条记录关键在这while(ors.next()){
ResultSet rs1=stmt.executeQuery("select * from Student");
while(rs1.next()){
if (ors.getString(1).equals(rs1.getString(1)))
continue;
stmt.executeUpdate("insert into Student values('"+ors.getString(1)+"','"+ors.getString(2)+"','"+ors.getString(3)+"','"+ors.getString(4)+"')");
各位朋友我该怎么写才能实现我想要的结果
运行结果是:
驱动sqlserver成功
连接sqlserver数据库成功
找到oracle驱动!
oracle驱动连接成功!
[Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
import java.io.*;
public class L_j {
public static void main(String[] args)throws IOException
{ test();}
public static String test(){
try
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String con="jdbc:microsoft:sqlserver://172.127.10.5:1433;DatabaseName=test";
String user="sa";
String password="";
Class.forName(driver);
System.out.println("驱动sqlserver成功");
Connection conn = DriverManager.getConnection(con,user,password);
System.out.println("连接sqlserver数据库成功");
Statement stmt=conn.createStatement();
String sql="";
Statement ostmt=oraclejdbc();
ResultSet ors=ostmt.executeQuery("select * from xxh_test");
while(ors.next()){
ResultSet rs1=stmt.executeQuery("select * from Student");
while(rs1.next()){
if (ors.getString(1).equals(rs1.getString(1)))
continue;
stmt.executeUpdate("insert into Student values('"+ors.getString(1)+"','"+ors.getString(2)+"','"+ors.getString(3)+"','"+ors.getString(4)+"')");
}
}
sql ="select * from Student";
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
int colcount=rsmd.getColumnCount();
while(rs.next())
{
for(int i=0;i<colcount;i++){
System.out.print(rs.getString(i+1)+"\t");
}System.out.println();
}
ors.close();
ostmt.close();
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
return null;
} /**create a method
*get oracle for jdbc
*connection
*insert . update. delete*/
public static Statement oraclejdbc(){
//Statement ostmt = null;
try
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("找到oracle驱动!");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print(e.getMessage());
}
String url="jdbc:oracle:thin:@web:1521:oradb";
Connection oconn=DriverManager.getConnection(url,"compiere","");
System.out.println("oracle驱动连接成功!");
Statement ostmt=oconn.createStatement();
return ostmt;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println(ex.getSQLState());
}
}return null;
}
}
我现在遇到的难题是从一张表插入到另一张表记录之前我想对其进行判断,如果该记录已有则不添加,可我的功能每次只能添加一条记录关键在这while(ors.next()){
ResultSet rs1=stmt.executeQuery("select * from Student");
while(rs1.next()){
if (ors.getString(1).equals(rs1.getString(1)))
continue;
stmt.executeUpdate("insert into Student values('"+ors.getString(1)+"','"+ors.getString(2)+"','"+ors.getString(3)+"','"+ors.getString(4)+"')");
各位朋友我该怎么写才能实现我想要的结果
运行结果是:
驱动sqlserver成功
连接sqlserver数据库成功
找到oracle驱动!
oracle驱动连接成功!
[Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
你baidu一下吧,问题很奇怪啊.
想要的话给我发邮件[email protected]