如何将JDBC连接数据库的代码改写为一个长尾词的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计212个文字,预计阅读时间需要1分钟。
javapackage test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;
public class DBConnection { String driver=com.mysql.jdbc.Driver; String url=jdbc:mysql://local;}
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class DBConnection{ String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test" String username="root"; String password="1234"; public static Connection getConnection(){ Connection conn = null; try{ Class.forName(driver); conn=DriverManager.getConnection(url,username,password) }catch(SQLException e){ e.printStackTrace(); } return conn; } public static Statement getStatement(Connection conn) throws ClassNotFoundException { Statement stmt = null; try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void closeConn(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void closeStmt(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void closeRs(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }
本文共计212个文字,预计阅读时间需要1分钟。
javapackage test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;
public class DBConnection { String driver=com.mysql.jdbc.Driver; String url=jdbc:mysql://local;}
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class DBConnection{ String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test" String username="root"; String password="1234"; public static Connection getConnection(){ Connection conn = null; try{ Class.forName(driver); conn=DriverManager.getConnection(url,username,password) }catch(SQLException e){ e.printStackTrace(); } return conn; } public static Statement getStatement(Connection conn) throws ClassNotFoundException { Statement stmt = null; try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void closeConn(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void closeStmt(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void closeRs(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }

