importjava.awt.Color;importjava.awt.Container;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionListener;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.sql.Statement;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;public class JavaJdbc extendsJFrame {
Connection connection=null;//驱动连接接口
PreparedStatement preparedstatement=null;//得到接口实现对象
ResultSet rs=null;//结果集接口
ResultSetMetaData rsmd=null;publicJavaJdbc(){try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //加载驱动
String dbUrl="jdbc:oracle:thin:@localhost:1521:orcl"; //连接oracle数据库
String username="admin"; //数据库的登录帐号和密码
String password="admin";
connection=DriverManager.getConnection(dbUrl,username,password);
String sql="select * from student";//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
preparedstatement=connection.prepareStatement(sql);
rs=preparedstatement.executeQuery();//获得结果集
rsmd=rs.getMetaData();int length=rsmd.getColumnCount();for(int i=1;i<=length;i++)
System.out.print(rsmd.getColumnName(i)+" ");
System.out.println();while(rs.next()){for(int i=1;i<=length;i++)
System.out.print(rs.getString(i)+" ");
System.out.println();
}
}catch(ClassNotFoundException e){//System.out.println("数据库驱动加载失败..."+e.getStackTrace());
e.printStackTrace();
}catch(SQLException e){
System.out.println("数据库连接失败..."+e.getStackTrace());
}finally{try{if(rs!=null){
rs.close();
}if(connection!=null){
connection.close();
}
}catch(SQLException e){
e.getStackTrace();
}
}
}public static voidmain(String args[]){newJavaJdbc();
}
}