zoukankan      html  css  js  c++  java
  • 手动连接数据库(jdbc)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
              String user="root";//数据库的用户名
               String pwd="";//数据库的密码
               String url="jdbc:mysql://localhost:3306/taotao";//taotao这个是你所要访问到数据库
               String driver="com.mysql.jdbc.Driver";
               Connection con=null;
               PreparedStatement stmt=null;
               ResultSet rs=null;
                try {
                    Class.forName(driver);
                    con=DriverManager.getConnection(url,user,pwd);
                    stmt=con.prepareStatement("select * from tb_item");//这里面是对应的sql语句,对应数据库中相应的表格
                     rs=stmt.executeQuery();
                     while(rs.next()){
                         System.out.println(rs.getObject(1)+","+rs.getObject(2));
                     }
                    
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    if(rs!=null){
                        rs.close();
                    }
                    if(stmt!=null){
                        stmt.close();
                    }
                    if(con!=null){
                        con.close();
                    }
                }
                
        }
    
    }

  • 相关阅读:
    epoll示例
    realloc的使用误区
    pyCharm最新激活码(2018激活码)
    python程序打包成.exe
    VS2017下载安装
    C# Cache缓存读取设置
    WPF中展示HTML
    Aspose Word模板使用总结
    js alert(“”)弹框 自定义样式
    利用反射将Model转化为sql
  • 原文地址:https://www.cnblogs.com/1314wamm/p/7280754.html
Copyright © 2011-2022 走看看