zoukankan      html  css  js  c++  java
  • jdbc for mysql demo

    package com.huawei.mysql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class MySqlTest {
    	public static void main(String[] args) throws SQLException {
    		Connection conn = null;
    		PreparedStatement stmt = null;
    		ResultSet rs = null;
    
    		try {
                Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(
    					"jdbc:mysql://localhost:3306/test", "root",
    					"123");
    			stmt = conn.prepareStatement("select * from person where id=?");
    			stmt.setInt(1, 10);
    			rs = stmt.executeQuery();
    			while (rs.next()) {
    				System.out.println(rs.getInt("id"));
    				System.out.println(rs.getString("name"));
    				System.out.println("----------------------------");
    			}
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			if (rs != null)
    				rs.close();
    			if (stmt != null)
    				stmt.close();
    			if (conn != null)
    				conn.close();
    		}
    	}
    }
    

      

  • 相关阅读:
    导出api文档
    Webservice测试从头来
    Java8新特性【转】
    spring获取bean的时候严格区分大小写
    java static 方法使用笔记
    maven Spring获取不到配置文件
    4月22日
    4月21日
    9月20日
    9月18日
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2152685.html
Copyright © 2011-2022 走看看