.
--
--
--
--
.
-
-
-
public class Demo01 {
public static void main(String[] args) throws Exception {
<!--DriverManager.registerDriver(new com.mysql.jdbc.Driver());-->
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/数据库名";
Connection connection = DriverManager.getConnection(url,"登录名","登录的密码");
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","123456");
Connection connection = DriverManager.getConnection(url,info);
String url1 ="jdbc:mysql://localhost:3306/数据库名?user=用户名&password=密码";
Connection connection = DriverManager.getConnection(null);
Statement statement = connection.createStatement();
String sql = "select * from users";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSex.next()){
System.out.println(resultSet.getObject(1));
System.out.println(resultSet.getObject(2));
System.out.println(resultSet.getObject(3));
System.out.println(resultSet.getObject(4));
System.out.println(resultSet.getObject(5));
System.out.println("_____________________");
}
resultSet.close();
statement.close();
connection.close();
public class DemoCRUD{
@Test
public void test throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/myjdbc"
Connection connection = DriverManager.getConnection(url,"root","123455");
Statement statement = connection.createStatement();
String sql ="insert into users values(2,'gs','123','123@qq.com','1993-1-1')";
int row = statement.executeUpdate(sql);
if(row>0){
System.out.println("插入成功");
}
}
}
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
public User() {
super();
}
public User(int id, String name, String password, String email, Date birthday) {
super();
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User[id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday="
+ birthday + "]";
}
}
public class DemoException {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhos:3306/myjdbc";
connection = DriverManager.getConnection(url,"root","123456");
statement = connection.createStatement();
String sql = "select * from users";
resultSet = statement.executeQuery(sql);
ArrayList<User> list = new ArrayList<>();
while(resultSet.next()){
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setEmail(resultSet.getString("email"));
user.setBirthday(resultSet.getDate("birthday"));
list.add(user);
}
for(User user :list){
System.out.println(user);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("获取连接失败");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("驱动加载失败");
}finally {
}
}
}
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:
user=root
password=123456
public class JDBCUtil {
private static String driverClass;
private static String url;
private static String user;
private static String password;
Properties properties = new Properties();
try{
FileInputStream fis = new FileInputStream("src/dbinfo.propeerties");
properties.load(fis);
driverClass = properties,getProperty("driverClass");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
}catech (Exception e) {
e.printStackTrace();
}
ResourceBundle rb = ResourceBundle.getBundle("dbinfo");
driverClass = rb.getString("driverClass");
url = rb.getString("url");
user = rb.getString("user");
password = rb.getString("password");
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){
if(resultSet != null){
try{
resultSet.close();
}catch (SQLException e){
throw new RuntimeException("关闭失败");
}
resultSet = null;
}if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException("关闭失败");
}
connection = null;
}
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException("关闭失败");
}
statement = null;
}
}
}