• JDBC for MySql5.5 简单示例


    package com.anllin.mysqltest;
    
    import java.sql.*;
    
    public class MysqlTest
    {
    	public static void main(String[] args)
    	{
    		connetMysqlTest();
    	}
    
    	public static void connetMysqlTest()
    	{
    		String connectionUrl = "jdbc:mysql://localhost:3306/mydata?user=root&password=123";
    
    		Connection conn = null;
    		Statement stmt = null;
    		ResultSet rs = null;
    
    		try
    		{
    			Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(connectionUrl);
    			conn.setAutoCommit(false);
    			stmt = conn.createStatement();
    			rs = stmt.executeQuery("select * from dept");
    			conn.commit();
    			System.out.print("deptno" + " ");
    			System.out.print("dname" + " ");
    			System.out.println("loc");
    			while (rs.next())
    			{
    				System.out.print(rs.getInt("deptno") + "      ");
    				System.out.print(rs.getString("dname") + "      ");
    				System.out.println(rs.getString("loc"));
    			}
    		}
    		catch (Exception e)
    		{
    			if(null != conn)
    			{
    				try
    				{
    					conn.rollback();
    				}
    				catch (SQLException e1)
    				{
    					e1.printStackTrace();
    				}
    			}
    			e.printStackTrace();
    		}
    		finally
    		{
    			if (null != rs)
    			{
    				try
    				{
    					rs.close();
    					rs = null;
    				}
    				catch (Exception e2)
    				{
    					e2.printStackTrace();
    				}
    			}
    
    			if (null != stmt)
    			{
    				try
    				{
    					stmt.close();
    					stmt = null;
    				}
    				catch (Exception e2)
    				{
    					e2.printStackTrace();
    				}
    			}
    
    			if (null != conn)
    			{
    				try
    				{
    					conn.close();
    					conn = null;
    				}
    				catch (Exception e2)
    				{
    					e2.printStackTrace();
    				}
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    RequireJS进阶(二)
    JavaScript判断元素为数字的奇异写法
    RequireJS进阶(三)
    RequireJS进阶(一)
    读Ext之十四(Ext元素)
    JavaScript中__proto__与prototype的关系
    工作流术语
    一个例子(Hello World)
    无题
    再谈调用子流程(1)
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2132171.html
Copyright © 2020-2023  润新知