数据库基础知识

    xiaoxiao2021-03-25  154

    几种数据库连接方式优缺点比较       能成功连接mysql的jdbc:   Class.forName( "com.mysql.jdbc.Driver" );   Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test" , "root" , "123456" );   Statement stmt = conn.createStatement();   ResultSet rs = stmt.executeQuery( "select * from employee" ); import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class HttpUtils {         public static void main(String [] args ) {                            java.sql.Connection con = null ;                            String url = "jdbc:mysql://localhost:3306/sq_cqzgd?"                 + "user=root&password=123456&useUnicode=true&characterEncoding=UTF8" ;                            ResultSet rs = null ;              java.sql.Statement stmt = null ;                             try {                //先加载驱动              Class.forName( "com.mysql.jdbc.Driver" );              } catch (ClassNotFoundException e ){                     System. out .println( "找不到驱动程序类,加载驱动失败!" );                      e .printStackTrace();              }                                  try {                  //创建数据库的连接             con =    java.sql.DriverManager.getConnection( url );              } catch (SQLException se ){                  System. out .println( "数据库连接失败!" );                  se .printStackTrace();               }                            try {                     //对数据库进行查询操作,返回时一个对象                stmt = con .createStatement();                rs = stmt .executeQuery( "select * from article" );                while ( rs .next()) {                         System. out .print( rs .getInt( "articleId" ) + "   " );                         System. out .print( rs .getInt( "userId" ) + "   " );                         System. out .println( rs .getString( "articleTitle" ) + "   " );                        }             } catch (SQLException e ){                System. out .println( "数据库操作失误!" );                e .printStackTrace();             }                         try {                  if ( rs != null ) {                   rs .close();                   rs = null ;                  }                  if ( stmt != null ) {                   stmt .close();                   stmt = null ;                  }                  if ( con != null ) {                   con .close();                   con = null ;                  }                 } catch (Exception e ) {                  System. out .println( "数据库关闭错误" );                  e .printStackTrace();                 }     } }
    转载请注明原文地址: https://ju.6miu.com/read-4439.html

    最新回复(0)