如何用java连接Openshift的java操作mysql数据库库

如何连接MySql中的数据库_百度知道
如何连接MySql中的数据库
java中连接MySql数据库
提问者采纳
} catch(ClassNotFoundException e) { System.println(&quot.&;
&#47.Driver&quot.,can`t find the Driver.isClosed())
conn.&-----------------------------------------&);sno& + & MySQL配置时的用户名
String password = &t&#92:mysql.println(&
System,到处都是这里不再写只提下几个注意点:1.-----------------------------------------&quot.createStatement(););
&#47.close()..getString(&quot.println(& 输出结果
rs,版本正确3;scutcs&/ 加载驱动程序Connection conn = DriverManager.printStackTrace().out!&
&#47.; 姓名&-----------------------------------------&quot.forName(driver).;
/ + &quot. } catch(Exception e) { e;sex&quot.; MySQL配置时的密码
String name = + name + &
/.println(&quot!
System.getString(& 结果集
/ +t&quot、连接数据库的java代码无错误还是贴个例子吧; 选择sname这列数据System、数据库启动;
&#47.,有能访问的账号密码2;\
/) + & 驱动程序名String url = &&#92.println(&quot!&quot:&#47.getString(&);S/
while(/Succeeded connecting to the Database.验证是否连接成功Statement statement =.、驱动的jar包位置正确;/
/);;sname&quot.;\jdbc.next()) {name =&#47.println(&quot.
/);); 学号&quot,t&性别&quot:& + & URL指向要访问的数据库名scutcs
String user = &quot.*, password);t&
S 连续数据库
if(.close().println(&/select * from student&quot.printStackTrace(); public class JDBCTest { public static void main(String[] args){ String driver = &);
/&#47.executeQuery(sql);/ 要执行的SQL语句ResultSet rs = statement.;.;\&#47.com.getConnection( } catch(SQLException e) { e;;&#47.jdbc.&#47.;
&#47.&#47java连接MySql的例子很多.. 执行结果如下所示;)); statement用来执行SQL语句String sql = &quot.println(rs.;localhost&#47.printStackTrace()
提问者评价
谢了,哥们
其他类似问题
mysql的相关知识
其他1条回答
&#47.Driver&).mm. public static Connection getConnection()
C用户名&);
return connection.getConnection(&quot.gft:org:数据库名&;主机.forName(&quot,&quot,&;**
* 与数据库建立连接
Connection connection = DriverManager:端口号;密码&jdbc:&#47
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁您所在的位置: &
Java的JDBC数据库连接池实现方法
Java的JDBC数据库连接池实现方法
虽然J2EE程序员一般都有现成的应用服务器所带的JDBC数据库连接池,不过对于开发一般的Java Application、 Applet或者JSP、velocity时,我们可用的JDBC数据库连接池并不多,并且一般性能都不好。
Java程序员都很羡慕Windows ADO ,只需要new Connection 就可以直接从数据库连接池中返回Connection。并且 ADO Connection 是线程安全的,多个线程可以共用一个Connection,所以ASP程序一般都把getConnection 放在 Global.asa 文件中,在 IIS 启动时建立数据库连接。ADO 的Connection 和Result 都有很好的缓冲,并且很容易使用。
其实我们可以自己写一个JDBC数据库连接池。
写JDBC connection pool 的注意事项有:
1. 有一个简单的函数从连接池中得到一个 Connection。
2. close 函数必须将connection 放回 数据库连接池。
3. 当数据库连接池中没有空闲的connection,数据库连接池必须能够自动增加connection 个数。
4. 当数据库连接池中的connection 个数在某一个特别的时间变得很大,但是以后很长时间只用其中一小部分,应该可以自动将多余的connection 关闭掉。
5. 如果可能,应该提供debug 信息报告没有关闭的new Connection 。
如果要new Connection 就可以直接从数据库连接池中返回Connection, 可以这样写( Mediator pattern ) (以下代码中使用了中文全角空格):public&class&EasyConnection&implements&java.sql.Connection{ &private&Connection&m_delegate&=&null; &public&EasyConnection(){ &m_delegate&=&getConnectionFromPool(); &} & public&void&close(){ &putConnectionBackToPool(m_delegate); &} &public&PreparedStatement&prepareStatement(String&sql)&throws&SQLException{ &m_delegate.prepareStatement(sql); &} &//......&other&method &}&
看来并不难。不过不建议这种写法,因为应该尽量避免使用Java Interface, 关于Java Interface 的缺点我另外再写文章讨论。大家关注的是Connection Pool 的实现方法。下面给出一种实现方法。import&java.sql.*; &import&java.lang.reflect.*; &import&java.util.*; &import&java.io.*; &&public&class&SimpleConnetionPool&{ &private&static&LinkedList&m_notUsedConnection&=&new&LinkedList(); &private&static&HashSet&m_usedUsedConnection&=&new&HashSet(); &private&static&String&m_url&=&""; &private&static&String&m_user&=&""; &private&static&String&m_password&=&""; &static&final&boolean&DEBUG&=&true; &static&private&long&m_lastClearClosedConnection&=&System.currentTimeMillis(); &public&static&long&CHECK_CLOSED_CONNECTION_TIME&=&4&*&60&*&60&*&<FONT color=#c;&&&static&{ &initDriver(); &} &&private&SimpleConnetionPool()&{ &} &&private&static&void&initDriver()&{ &Driver&driver&=&null; &&try&{ &driver&=&(Driver)&Class.forName("com.mysql.jdbc.Driver").newInstance(); &installDriver(driver); &}&catch&(Exception&e)&{ &} &&&try&{ &driver&=&(Driver)&Class.forName("org.postgresql.Driver").newInstance(); &installDriver(driver); &}&catch&(Exception&e)&{ &} &} &&public&static&void&installDriver(Driver&driver)&{ &try&{ &DriverManager.registerDriver(driver); &}&catch&(Exception&e)&{ &e.printStackTrace(); &} &} &&&public&static&synchronized&Connection&getConnection()&{ &clearClosedConnection(); &while&(m_notUsedConnection.size()&&&0)&{ &try&{ &ConnectionWrapper&wrapper&=&(ConnectionWrapper)&m_notUsedConnection.removeFirst(); &if&(wrapper.connection.isClosed())&{ &continue; &} &m_usedUsedConnection.add(wrapper); &if&(DEBUG)&{ &wrapper.debugInfo&=&new&Throwable("Connection&initial&statement"); &} &return&wrapper. &}&catch&(Exception&e)&{ &} &} &int&newCount&=&getIncreasingConnectionCount(); &LinkedList&list&=&new&LinkedList(); &ConnectionWrapper&wrapper&=&null; &for&(int&i&=&0;&i&&&newC&i++)&{ &wrapper&=&getNewConnection(); &if&(wrapper&!=&null)&{ &list.add(wrapper); &} &} &if&(list.size()&==&0)&{ &return&null; &} &wrapper&=&(ConnectionWrapper)&list.removeFirst(); &m_usedUsedConnection.add(wrapper); &&m_notUsedConnection.addAll(list); &list.clear(); &&return&wrapper. &} &&private&static&ConnectionWrapper&getNewConnection()&{ &try&{ &Connection&con&=&DriverManager.getConnection(m_url,&m_user,&m_password); &ConnectionWrapper&wrapper&=&new&ConnectionWrapper(con); &return& &}&catch&(Exception&e)&{ &e.printStackTrace(); &} &return&null; &} &&static&synchronized&void&pushConnectionBackToPool(ConnectionWrapper&con)&{ &boolean&exist&=&m_usedUsedConnection.remove(con); &if&(exist)&{ &m_notUsedConnection.addLast(con); &} &} &&public&static&int&close()&{ &int&count&=&0; &&Iterator&iterator&=&m_notUsedConnection.iterator(); &while&(iterator.hasNext())&{ &try&{ &(&(ConnectionWrapper)&iterator.next()).close(); &count++; &}&catch&(Exception&e)&{ &} &} &m_notUsedConnection.clear(); &&iterator&=&m_usedUsedConnection.iterator(); &while&(iterator.hasNext())&{ &try&{ &ConnectionWrapper&wrapper&=&(ConnectionWrapper)&iterator.next(); &wrapper.close(); &if&(DEBUG)&{ &wrapper.debugInfo.printStackTrace(); &} &count++; &}&catch&(Exception&e)&{ &} &} &m_usedUsedConnection.clear(); &&return& &} &&private&static&void&clearClosedConnection()&{ &long&time&=&System.currentTimeMillis(); &&if&(time&&&m_lastClearClosedConnection)&{ &time&=&m_lastClearClosedC &return; &} &&if&(time&-&m_lastClearClosedConnection&&&CHECK_CLOSED_CONNECTION_TIME)&{ &return; &} &m_lastClearClosedConnection&=& &&&Iterator&iterator&=&m_notUsedConnection.iterator(); &while&(iterator.hasNext())&{ &ConnectionWrapper&wrapper&=&(ConnectionWrapper)&iterator.next(); &try&{ &if&(wrapper.connection.isClosed())&{ &iterator.remove(); &} &}&catch&(Exception&e)&{ &iterator.remove(); &if&(DEBUG)&{ &System.out.println("connection&is&closed,&this&connection&initial&StackTrace"); &wrapper.debugInfo.printStackTrace(); &} &} &} &&&int&decrease&=&getDecreasingConnectionCount(); &if&(m_notUsedConnection.size()&&&decrease)&{ &return; &} &&while&(decrease--&&&0)&{ &ConnectionWrapper&wrapper&=&(ConnectionWrapper)&m_notUsedConnection.removeFirst(); &try&{ &wrapper.connection.close(); &}&catch&(Exception&e)&{ &} &} &} &&&&&&public&static&int&getIncreasingConnectionCount()&{ &int&count&=&1; &int&current&=&getConnectionCount(); &count&=&current&/&4; &if&(count&&&1)&{ &count&=&1; &} &return& &} &&&&&&public&static&int&getDecreasingConnectionCount()&{ &int&count&=&0; &int&current&=&getConnectionCount(); &if&(current&&&10)&{ &return&0; &} &return&current&/&3; &} &&public&synchronized&static&void&printDebugMsg()&{ &printDebugMsg(System.out); &} &&public&synchronized&static&void&printDebugMsg(PrintStream&out)&{ &if&(DEBUG&==&false)&{ &return; &} &StringBuffer&msg&=&new&StringBuffer(); &msg.append("debug&message&in&"&+&SimpleConnetionPool.class.getName()); &msg.append("\r\n"); &msg.append("total&count&is&connection&pool:&"&+&getConnectionCount()); &msg.append("\r\n"); &msg.append("not&used&connection&count:&"&+&getNotUsedConnectionCount()); &msg.append("\r\n"); &msg.append("used&connection,&count:&"&+&getUsedConnectionCount()); &out.println(msg); &Iterator&iterator&=&m_usedUsedConnection.iterator(); &while&(iterator.hasNext())&{ &ConnectionWrapper&wrapper&=&(ConnectionWrapper)&iterator.next(); &wrapper.debugInfo.printStackTrace(out); &} &out.println(); &} &&public&static&synchronized&int&getNotUsedConnectionCount()&{ &return&m_notUsedConnection.size(); &} &&public&static&synchronized&int&getUsedConnectionCount()&{ &return&m_usedUsedConnection.size(); &} &&public&static&synchronized&int&getConnectionCount()&{ &return&m_notUsedConnection.size()&+&m_usedUsedConnection.size(); &} &&public&static&String&getUrl()&{ &return&m_ &} &&public&static&void&setUrl(String&url)&{ &if&(url&==&null)&{ &return; &} &m_url&=&url.trim(); &} &&public&static&String&getUser()&{ &return&m_ &} &&public&static&void&setUser(String&user)&{ &if&(user&==&null)&{ &return; &} &m_user&=&user.trim(); &} &&public&static&String&getPassword()&{ &return&m_ &} &&public&static&void&setPassword(String&password)&{ &if&(password&==&null)&{ &return; &} &m_password&=&password.trim(); &} &&} &&class&ConnectionWrapper&implements&InvocationHandler&{ &private&final&static&String&CLOSE_METHOD_NAME&=&"close"; &public&Connection&connection&=&null; &private&Connection&m_originConnection&=&null; &public&long&lastAccessTime&=&System.currentTimeMillis(); &Throwable&debugInfo&=&new&Throwable("Connection&initial&statement"); &&ConnectionWrapper(Connection&con)&{ &this.connection&=&(Connection)&Proxy.newProxyInstance( &con.getClass().getClassLoader(), &con.getClass().getInterfaces(),&this); &m_originConnection&=& &} &&void&close()&throws&SQLException&{ &m_originConnection.close(); &} &&public&Object&invoke(Object&proxy,&Method&m,&Object[]&args)&throws&Throwable&{ &Object&obj&=&null; &if&(CLOSE_METHOD_NAME.equals(m.getName()))&{ &SimpleConnetionPool.pushConnectionBackToPool(this); &} &else&{ &obj&=&m.invoke(m_originConnection,&args); &} &lastAccessTime&=&System.currentTimeMillis(); &return& &} &}&
使用方法public&class&TestConnectionPool{ &public&static&void&main(String[]&args)&{ &SimpleConnetionPool.setUrl(DBTools.getDatabaseUrl()); &SimpleConnetionPool.setUser(DBTools.getDatabaseUserName()); &SimpleConnetionPool.setPassword(DBTools.getDatabasePassword()); &&Connection&con&=&SimpleConnetionPool.getConnection(); &Connection&con1&=&SimpleConnetionPool.getConnection(); &Connection&con2&=&SimpleConnetionPool.getConnection(); &&//do&something&with&con&... &&try&{ &con.close(); &}&catch&(Exception&e)&{} &&try&{ &con1.close(); &}&catch&(Exception&e)&{} &&try&{ &con2.close(); &}&catch&(Exception&e)&{} &&con&=&SimpleConnetionPool.getConnection(); &con1&=&SimpleConnetionPool.getConnection(); &try&{ &con1.close(); &}&catch&(Exception&e)&{} &&con2&=&SimpleConnetionPool.getConnection(); &SimpleConnetionPool.printDebugMsg(); &&} &}&
运行测试程序后打印JDBC数据库连接池中Connection状态,以及正在使用的没有关闭Connection信息。
【编辑推荐】
【责任编辑: TEL:(010)】
关于的更多文章
Angular.js 是一个MV*(Model-View-Whatever,不管是MVC或者MVVM
本次的专刊为大家提供了Oracle最新推出的Java SE 8详细的开发教程,从解读到探究Java 8最新
国庆假期前的612,是不是有点折腾?网上的中秋国庆放
刚刚开始接触java数组的人都会听到一句类似的话:java
9月的工作日被法定假日拆的零零散散,不知道各位的工
《网络系统开发实例精粹》以实际的软件开发项目实例介绍贯穿始末,逐层深入的介绍了应用JSP开发Web应用程序的详细过程。全书以深
51CTO旗下网站怎么用java连接数据库?_百度知道
怎么用java连接数据库?
提问者采纳
OracleDriver:driverName的值为;url的值为:1521.executeQuery();hibernate其中hibernate是你的数据库名称2.sqlserver:1433;DatabaseName=&quot.1:3306&#47,然后要注册驱动首先需要导入相应数据库的驱动文件;localhost.0:jdbc.oracle.url的值.0;url的值为jdbc.executeQuery():oracle.如果要使用到返回集合则可以用ResultSet对象接收.getPreparedState(不要使用到返回集合的话就直接pst:driverName的值为;localhost.forname(driverName):driverName的值为,其中database是你数据库名称3,获取Connection对象Connection conn=DriverM + database.sqlserver:@127;然后获取PreparedStatement对象PreparedStatement pst=conn.driver.jdbc:sqlserver:com:thin:&#47.以上的可以查jdk1.microsoft.getConnection(URL);&#47:mysql:oracle.SQLServerDriver.&#47,ResultSet rs=pst,password)其中的username和password是你访问数据库的名称和密码,username:&#47,其中ora92是你数据库名称:jdbc:com:ora92.Driver,Class
提问者评价
thanks不明白
其他类似问题
连接数据库的相关知识
您可能关注的推广回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 java读取mysql数据库 的文章

 

随机推荐