首页 > 代码库 > jdbc练习

jdbc练习

package com.jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.SynchronousQueue;
import java.sql.Connection;


import com.mysql.jdbc.Driver;

public class demo1 {
public static void main(String[] args) throws SQLException {
//1.加载驱动--注册驱动
DriverManager.registerDriver(new Driver());
//2.建立连接
String url = "jdbc:mysql://locahost:3306/day07";
String user = "root";
String password = "12345";
Connection conn = DriverManager.getConnection(url, user, password);
//3.获取statment对象
Statement stmt = conn.createStatement();
//4、向数据库发送sql--执行sql
String sql = "selcet*from user";
ResultSet rs = stmt.executeQuery(sql);
//5、处理结果集
while(rs.next()){
System.out.println(rs.getString("name"));
}
//6、释放资源,关闭连接
rs.close();
stmt.close();
conn.close();
}
}

jdbc练习