可按Ctrl+D收藏 蚂蚁资源网

蚂蚁资源网

jdbc源码(shardingjdbc源码分析)

  • 时间:2021-02-12 00:14 编辑:柳成勋 来源:蚂蚁资源 阅读:244
  • 扫一扫,手机访问
摘要:大家好,今天给大家介绍关于jdbc源码(shardingjdbc源码分析)的相关内容,详细讲解如何找到mysql,jdbc编程那一集的源码在哪里下,用java语言编写的jdbc数据库与java集合框架相连接的程序源代码等,希望可以帮助到您。
如何找到mysql,右键单击项目中的新文件,名为jdbc.properties,如图所示:在jdbc.properties文件中输入以下信息,该文件是数据库驱动程序,连接,用户名和密码。 new jdbctest2.java类输入以下代码:代码描述:此代码读取配置文件,通过名称读取配置文件中的各种项目,通过反射,创建驱动程序对象,反射是主的实例化主机函数,测试方法运行结果如下,表示成功连接!
jdbc编程那一集的源码在哪里下,通过,请回答专业人士。哦,我爱男人的女朋友11集http://11.guami.cc
用java语言编写的jdbc数据库与java集合框架相连接的程序源代码,我使用了,Oracle数据库:包com.icool.common.util;导入java.sql.connection;导入java.sql.drivermanager;导入java.sql.preparedstatement;导入java.sql.resultset;导入Java。 sql.sqlexception;导入java.sql.statement; / ** * * * @author zh_q * @version 1.0 * / public类getconn {private connection conn = null;私有字符串USNAME;私有字符串USPWD;私有字符串CLFN;私有字符串DMNAME; //呼叫空的人参构造函数,默认值启用本地数据库公共getconn(this.clfn =“Oracle.jdbc.driver.oracledriver”; this.dmname =“jdbc:oracle:then:then:@localhost:1521:orcl; orcl; thisuspwd = “Q792002998”这个;这个;Usname =“system”;} / ** * @return数据库连接对象* / public connection getconn(); conn = drivermanager.getConnection(Dmname,usname,uspwd;} catch(classnotfoundexception e){e.printstacktrace();捕获(sqllexception e){e.printstacktrace();} return conn;} / ** * @return退出根据sql查询语句结果* @throws sqlexception * /公共结果集执行查询(String SQL)抛出SQLException {CONN = GETCON();声明stmt = conn.createstatement(); ResultSet RS = stmt.executeQuery(SQL);返回rs;} / ** * @return影响行数* @throws sqlexception * / public int ExecuteUpdate(String SQL)抛出SQLException {语句STMT = NULL INT I = 0; getconn(); stmt = conn.createstatement(); i = stmt.executeUpdate(SQL); returni} / ** * @return返回预编译对象* @throws sqlexception * / public prepardstatement prepartstatement(string sql)抛出sqlexception {prepardstatement pstmt = null; getconn(); pstmt = conn.prepleStatement(SQL);返回pstmt;} / ** * @param关闭连接* @throws databasexpection * / public void close(){if(conn!= null){try {conn.close();捕获(sqlxception e){e。 printstacktrace();}} / ** * @param设置是否自动提交* @throws sqlexception * / public void setautocommit(boolean b)抛出sqlexception {getconn(); conn(); conn(); conlic voidback(); conlic voidback(); (); conn.rollback(); conn.rollback(); }

责任编辑(柳成勋

以上就是关于**jdbc源码,shardingjdbc源码分析**的全部内容,了解更多请关注蚂蚁资源网。
  • 全部评论(3)
  • 大少爷
  • JDBC实现连接数据库中表的代码,package com.b510;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * * @author Hongten * @date 2012-7-16 * */public class JDBCTest { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String dbName = "spring"; String passwrod = "root"; String userName = "root"; String url = "jdbc:mysql://localhost:3308/" + dbName; String sql = "select * from users";try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, userName, passwrod); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("id : " + rs.getInt(1) + " name : " + rs.getString(2) + " password : " + rs.getString(3)); }// 关闭记录集 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } }// 关闭声明 if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } }// 关闭链接对象 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }} catch (Exception e) { e.printStackTrace(); } }}
  • 2021-02-12 00:14:57
  • wdq
  • JDBC连接Access第一种方法: 你用下面的代码试试 (强烈建议也是only可以建议的方法) con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/data/Access/test1.mdb","dba","sql"); 后面的代码一样
  • 2021-02-12 00:14:57
  • 893811145
  • ]** 操作数据库元数据的服务类* @author RanJi* @date 2013-10-1* @since JDK1.7* @version 1.0*/public class JdbcUtil {private static Logger logger = Logger.getLogger(JdbcUtil.class);private static JdbcUtil jdbcUtil;private Connection con;private JdbcUtil(DataBaseType dbType, String hostAddr, String port, String dbName, String userName, String password){String driverName = "";String jdbcURL = "";String dbTypeName = "";switch (dbType.getValue()) {case 0:dbTypeName = "Oracle DataBase";driverName = "oracle.jdbc.driver.OracleDriver ";jdbcURL = "jdbc:oracle:thin:@"+hostAddr+":"+port;if(dbName !=null || !"".equals(dbName)) jdbcURL += ":"+dbName;break;case 1:dbTypeName = "MySQL DataBase";driverName = "com.mysql.jdbc.Driver";jdbcURL = "jdbc:mysql://"+hostAddr+":"+port;if(dbName !=null || !"".equals(dbName)) jdbcURL += "/"+dbName;break;case 2:dbTypeName = "DB2 DataBase";driverName = "com.ibm.db2.jcc.DB2Driver";jdbcURL = "jdbc:db2://"+hostAddr+":"+port;if(dbName !=null || !"".equals(dbName)) jdbcURL += "/"+dbName;break;case 3:dbTypeName = "SyBase DataBase";driverName = "com.sybase.jdbc3.jdbc.SybDriver";jdbcURL = "jdbc:sybase:Tds:"+hostAddr+":"+port;if(dbName !=null || !"".equals(dbName)) jdbcURL += "/"+dbName;break;case 4:dbTypeName = "SQLServer DataBase";driverName = "net.sourceforge.jtds.jdbc.Driver"; //--这里注意,连接MSSQL数据库没有使用官方驱动,而是使用了第三方的驱动jdbcURL = "jdbc:jtds:sqlserver://"+hostAddr+":"+port;if(dbName !=null || !"".equals(dbName)) jdbcURL += "/"+dbName;break;}try{Class.forName(driverName);con = DriverManager.getConnection(jdbcURL,userName,password);}catch(ClassNotFoundException e) {logger.info("DataBase Connection Driver Class: " + driverName + " Does Not Exist.");}catch(SQLException e){logger.info("Sorry,The DataBase Connection is Unsuccessful,Please Check The IP Address、The DataBase Name、UserName And Password Are Correct?");}logger.info("Congratulations, Connection " + dbTypeName + " is Successful.");}/*** 单例设计模式的应用* @param dbType* @param hostAddr* @param port* @param dbName* @param userName* @param password* @return*/public static JdbcUtil getInstance(DataBaseType dbType, String hostAddr, String port, String dbName, String userName, String password){if(jdbcUtil == null) jdbcUtil = new JdbcUtil(dbType, hostAddr, port, dbName, userName, password);return jdbcUtil;}public Connection getConnection(){return con;}/*** 内部使用的释放资源的方法* @param rs* @param sm* @param con*/private void close(ResultSet rs, Statement sm, Connection con){if(rs != null)try {rs.close();} catch (SQLException e) {e.printStackTrace();}finally{if(sm != null)try {sm.close();} catch (SQLException e) {e.printStackTrace();} finally{if(con != null)try {con.close();} catch (SQLException e) {e.printStackTrace();}}}}/*** 获取有几个数据库(名称)* @param con* @return*/public List<String> getDataBases(){List<String> dataBases = new ArrayList<String>();ResultSet rs = null;if(con != null){try {DatabaseMetaData dbMetaData = con.getMetaData();rs = dbMetaData.getCatalogs();while(rs.next()){dataBases.add(rs.getString(1));}} catch (SQLException e) {e.printStackTrace();} finally{close(rs,null,null);}}return dataBases;}public List<String> getTables(String dataBaseName){List<String> tables = new ArrayList<String>();ResultSet rs = null;if(con != null){try {DatabaseMetaData dbMetaData = con.getMetaData();rs = dbMetaData.getTables(dataBaseName, null, null, new String[]{"TABLE","VIEW"});while(rs.next()){tables.add(rs.getString("TABLE_NAME"));}} catch (SQLException e) {e.printStackTrace();} finally{close(rs,null,null);}}return tables;}public List<String> getColumns(String tableName,String dataBase){List<String> columns = new ArrayList<String>();ResultSet rs = null;if(con != null){try {DatabaseMetaData dbMetaData = con.getMetaData();rs = dbMetaData.getColumns(dataBase, null, tableName, null);while(rs.next()){columns.add(rs.getString("COLUMN_NAME")+":" + rs.getString("TYPE_NAME") + ":" +rs.getString("COLUMN_SIZE"));}} catch (SQLException e) {e.printStackTrace();} finally{close(rs,null,null);}}return columns;}/*** 创建数据库* @param dbName 数据库名称*/public void createDB(String dbName){String sql = "CREATE DATABASE "+dbName;Statement stmt = null;if(con != null) {try {stmt = con.createStatement();stmt.executeUpdate(sql);} catch (SQLException e) {logger.info("Sorry,DataBase `"+dbName+"` Already Exists.");return;} finally{close(null,stmt,null);}}logger.info("Congratulations, Creatae DataBase `"+dbName+"` Is Successful.");}/*** 执行SQL语句的方法* @return*/public void excuteSQL(String sql,String dbName){Statement stmt = null;if(con != null){try {con.setCatalog(dbName);} catch (SQLException e) {logger.info("Sorry,DataBase `"+dbName+"` Does Not Exists,Please Check And Given The Existence Of The DataBase.");return;}try {stmt = con.createStatement();stmt.executeUpdate(sql);logger.info("Excute SQL Statement: `"+sql+"` Is Successful.");} catch (SQLException e) {e.printStackTrace();} finally{close(null,stmt,null);}}}public String getJSONStrDataBase(String dbName){return getJSONStrDataBase(dbName, "name", "tables", "columns");}public String getJSONStrDataBase(String dbName,String CommonNodeNameKey,String TablesChildrenNodeNamekey,String columnsChildrenNodeNameKey){StringBuilder dbStr = new StringBuilder();StringBuilder tableStr = new StringBuilder();StringBuilder columnStr = new StringBuilder();ResultSet tableRS = null;ResultSet columnRS = null;boolean tableFlag = false;boolean columnFlag = false;if(con != null){try {DatabaseMetaData dbMetaData = con.getMetaData();dbStr.append("{\""+CommonNodeNameKey+"\":");dbStr.append("\""+dbName+"\",\""+TablesChildrenNodeNamekey+"\":[");tableRS = dbMetaData.getTables(dbName, null, null, new String[]{"TABLE","VIEW"});while(tableRS.next()){tableFlag = true;tableStr.append("{\""+CommonNodeNameKey+"\":");tableStr.append("\""+tableRS.getString("TABLE_NAME")+"\",\""+columnsChildrenNodeNameKey+"\":[");columnRS = dbMetaData.getColumns(dbName, null, tableRS.getString("TABLE_NAME"), null);while(columnRS.next()){columnFlag = true;columnStr.append("{\""+CommonNodeNameKey+"\":");columnStr.append("\""+columnRS.getString("COLUMN_NAME")+"\""+",");columnStr.append("\"type\":");columnStr.append("\""+columnRS.getString("TYPE_NAME")+"\""+",");columnStr.append("\"size\":");columnStr.append(columnRS.getString("COLUMN_SIZE"));columnStr.append("},");}if(columnFlag){columnFlag = false;columnStr = columnStr.replace(columnStr.length()-1, columnStr.length(), "");tableStr.append(columnStr);columnStr = new StringBuilder();}tableStr.append("]");tableStr.append("},");}dbStr.append(tableStr);if(tableFlag)dbStr = dbStr.replace(dbStr.length()-1, dbStr.length(), "");dbStr.append("]}");} catch (SQLException e) {e.printStackTrace();} finally{close(columnRS,null,null);close(tableRS,null,null);}}return dbStr.toString();}/*** 外部使用的释放资源的方法 (最后被调用的方法,以便释放资源)*/public void close(){if(con != null)try {con.close();} catch (SQLException e) {e.printStackTrace();}}}
  • 2021-02-24 12:50:01
最新发布的资讯信息
【简历/资料|内地女明星】 殷茹基本资料( YR个人简历介绍)(2020-12-06 15:19)
【简历/资料|内地女明星】 曹菁基本资料( CJ个人简历介绍)(2020-12-06 15:18)
【简历/资料|内地女明星】 王安妮基本资料( WAN个人简历介绍)(2020-12-06 15:18)
【简历/资料|内地女明星】 白琼基本资料( BQ个人简历介绍)(2020-12-06 15:17)
【简历/资料|内地女明星】 王世霞基本资料( WSX个人简历介绍)(2020-12-06 15:17)
【简历/资料|内地女明星】 宋煜基本资料( SY个人简历介绍)(2020-12-06 15:16)
【简历/资料|内地女明星】 钱增基本资料( QZ个人简历介绍)(2020-12-06 15:16)
【简历/资料|内地女明星】 胡晓黎基本资料( HXL个人简历介绍)(2020-12-06 15:15)
【简历/资料|内地女明星】 李佳慧基本资料( LJH个人简历介绍)(2020-12-06 15:15)
【简历/资料|内地女明星】 张洛嘉基本资料( ZLJ个人简历介绍)(2020-12-06 15:14)
联系客服
网站客服 联系客服
手机版

扫一扫进手机版
返回顶部