如何连接Mysql的jdbc
这篇文章将为大家详细讲解有关如何连接Mysql的jdbc,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
packageutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;publicclassDBUtil{Connectionconn=null;PreparedStatementstmt=null;StringDriver="com..jdbc.Driver";Stringurl="jdbc:mysql://10.137.5.23/mcip?useUnicode=true&characterEncoding=GBK";Stringuser="mcip";Stringpassword="mcip";ResultSetrs=null;//完成连接的创建publicConnectiongetConnection()throwsException{Class.forName(Driver);if(conn==null){conn=DriverManager.getConnection(url,user,password);}returnconn;}//创建语句对象publicPreparedStatementcreateStatement(Stringsql)throwsException{stmt=getConnection().prepareStatement(sql);returnstmt;}//执行有结果集返回的方法publicResultSetexcuteQuery()throwsException{rs=stmt.executeQuery();returnrs;}//执行没有结果集返回的方法publicintexcuteUpdate()throwsException{returnstmt.executeUpdate();}//关闭对象publicvoidclose(){if(rs!=null)try{rs.close();}catch(Exceptione){}if(stmt!=null)try{stmt.close();}catch(Exceptione){}if(conn!=null)try{conn.close();}catch(Exceptione){}}}
关于“如何连接Mysql的jdbc”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。