zoukankan      html  css  js  c++  java
  • JDBC 简单数据库操作

    1 package cose.seu.edu.cn.cosersoft;
    2 import java.sql.*;
    3 public class Test {
    4 /*
    5 * Create a connection to MYSQL database
    6 */
    7 public static Connection getConnection()
    8 throws ClassNotFoundException, SQLException{
    9 String url="jdbc:mysql://127.0.0.1:3306/myDb";
    10 Class.forName("com.mysql.jdbc.Driver");
    11 String username="root";
    12 String password="coser";
    13 Connection conn = DriverManager.getConnection(url, username, password);
    14 return conn;
    15 }
    16 public static void PrintResult(ResultSet rs) throws SQLException{
    17 while(rs.next()){
    18 System.out.println(rs.getInt(1));
    19 }
    20 }
    21 public static void main(String [] args) throws Exception{
    22 Connection conn = getConnection();
    23 Statement sqlstate = conn.createStatement();
    24 if(!conn.isClosed())
    25 System.out.println("连接数据库成功");
    26 else System.out.println("连接数据库失败");
    27 //Execute common sql text;
    28 String sqltext = "select * from mytb";
    29 ResultSet rs = sqlstate.executeQuery(sqltext);
    30 PrintResult(rs);
    31 sqlstate.close();
    32 //Call procedure with no parameters
    33 CallableStatement cs = conn.prepareCall("{call myPro()}");
    34 rs=cs.executeQuery();
    35 PrintResult(rs);
    36 cs.close();
    37 //Call procedure with parameters
    38 CallableStatement cs2 = conn.prepareCall("{call pr_Add(?)}");
    39 //add parameter
    40 cs2.setInt(1, 1001);
    41 cs2.execute();
    42 System.out.println("Insert successfully!");
    43 cs2.close();
    44 conn.close();
    45 }
    46 }
  • 相关阅读:
    区块链技术栈-区块链账本
    (引文)可扩展的分布式数据库架构
    CentOS7 通过systemd 添加开机重启服务
    spring发布RMI服务(-)
    使用jdbc连接上oracle的两种方法
    用户态和内核态
    MySQL数据库备份还原(基于binlog的增量备份)
    分布式事务
    shuffle 过程
    MapReduce的流程
  • 原文地址:https://www.cnblogs.com/coser/p/2073304.html
Copyright © 2011-2022 走看看