连接数据库的步骤

1.加载驱动程序 
Class.forName("oracle.jdbc.driver.OracleDriver"); 
2. 
建立连接 
Connection conn = DriverMananger.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","用户名","密码"); 
3 执行查询
4 关闭数据库连接
/连接之前,先现在驱动mysql-connector-java-3.1.12.zip解压把其中的
//mysql-connector-java-3.1.12-bin.jar加入到项目中
import java.sql.*;
public class DbUtil {
   public static void main(String[] args){
       String driver = "com.mysql.jdbc.Driver";     //驱动
       String url = "jdbc:MySql://localhost/login"; //数据库URL
       String user="root";                          //数据库登录名
       String password = "mysql";                   //数据库密码
 try {
 Class.forName(driver);
 Connection dcon = DriverManager.getConnection(url,user,password);
 String sql = "select * from login;";
 Statement stmt = dcon.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
      while(rs.next())
        {
     System.out.print("id:"+rs.getInt("id")+",姓名:"+rs.getString("name"));
        }
    }
  catch (Exception e)
   {
 e.printStackTrace();
 }
}}


你可能感兴趣的:(数据库,exception,mysql,String,jdbc,url)