JDBC
时间:2021-07-01 10:21:17
帮助过:29人阅读
void test()
throws Exception{
// register the driver
//DriverManager.register(new com.mysql.jdbc.Driver()); ----------> not recommended
Class.forName("com.mysql.jdbc.Driver()");
// get the connection of database
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day15","root","root"
);
// create statement object
Statement statement =
connection.createStatement();
// execute the sql statement
ResultSet results = statement.executeQuery(‘SELECT * FROM users‘
);
// get the information in the table
while(results.next()){
String id = results.getObject("id"
);
String name = results.getObject("name"
);
String password = results.getObject("password"
);
String email = results.getObject("email"
);
String birthday = results.getObject("birthday"
);
}
// close the resource
results.close();
statement.close();
connection.close();
}
DriverManager:
function:
1. register the driver:
Class.forName("com.mysql.jdbc.Driver");
2. get the connection:
Connection connection = DriverManager.getConnecion("jdbc:mysql://localhost:3306/mydb1","root","root");
three ways to get the connection:
method1:
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","root");
method2:
Properties prop = new Properties();
prop.put("user","root");
prop.put("password","root");
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1",prop);
method3:
DriverManager.getConnection("jdbc:mysql://localhost:3306?user=root&password=root");
JDBC
标签:cio name rom word set password javaee mail fun