用Java开发C/S应用(六):swt中连接数据库

连接数据库的话,需要先把jar包拷到应用中去,我连的是oracle数据库,使用ojdbc14.jar包

                 try  {
                    Class.forName(
" oracle.jdbc.driver.OracleDriver " ).newInstance(); 
                    Connection conn
=  DriverManager.getConnection( " jdbc:oracle:thin:@127.0.0.1:1521:orcl " , "username " , "password " ); 
                    Statement stmt
= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
                    String sql
= " select * from table1 "
                    ResultSet rs
= stmt.executeQuery(sql); 
                    
while (rs.next())
                    {
                        System.out.println(rs.getString(
" FieldName " ));
                    }
                    rs.close(); 
                    stmt.close(); 
                    conn.close();
                } 
catch  (Exception e1) {
                    e1.printStackTrace();
                } 
代码很简单,就是普通的查询表的功能,这里就不做介绍了

转载于:https://www.cnblogs.com/modou/articles/1526996.html

你可能感兴趣的:(用Java开发C/S应用(六):swt中连接数据库)