zoukankan      html  css  js  c++  java
  • Java基础代码库:JDBC连接MySQL

    import java.sql.*;
    public class mysql
    {
        public static void main(String[] args)
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/test";
                String userName = "root";
                String passwd = "root";
                Connection con = DriverManager.getConnection(url, userName, passwd);
                Statement st = con.createStatement();
                String query = "select * from user;";
                ResultSet rs = st.executeQuery(query);
                while(rs.next())
                {
                    int num = rs.getInt("id");
                    String name = rs.getString("username");
                    System.out.println(num+" "+name);
                }
                rs.close();
                st.close();
                con.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Python 虚拟环境(VirtualEnv)
    python 枚举
    Python 面向对象编程
    Python 使用模块
    Python 函数
    Python dict & set
    JAVA-工具类
    09-12 练习题
    JAVA-数组
    java-语句
  • 原文地址:https://www.cnblogs.com/todsong/p/2636981.html
Copyright © 2011-2022 走看看