application下的JDBC操作

新建Java project名称为TextJDBC,在src文件夹下新建com.zss.www的包,并在包里建个class:UseDadabase

在MySQL中建立一个lb的库,并在其建立表:student,表中包涵name,age,sex

UseDadabase代码如下:

package com.zss.www;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.jdbc.Connection;

public class UseDadabase {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/lb";
        String username="root";
        String password="11";
        Connection conn=null;
        Class.forName(driver);
        conn=(Connection) DriverManager
                .getConnection(url,username,password);
        Statement stmt=null;
        ResultSet rs=null;
        stmt=(Statement) conn.createStatement();
        rs=stmt.executeQuery("select*from student");
        while(rs.next()){
            System.out.println("name="+rs.getString(1));
            System.out.println("age="+rs.getString(2));
            System.out.println("sex="+rs.getString(3));
        }
        rs.close();
        stmt.close();
        conn.close();
        


    }

}

 

你可能感兴趣的:(application下的JDBC操作)