20201103马伟婷
知识总结:
/*用main测试*/
public static void main(String[] args)
try
save();
catch (Exception e)
e.printStackTrace();
/*定义添加用户的方法*/
public static void save() throws Exception {
1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
2、创建数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mirabda","root","");
3、拼写sql语句
String sql = "INSERT INTO t-table
(userName,passWord
,gender)VALUES('宫本武藏','rb123','女')";
4、发送sql
PreparedStatement prepareStatement = connection.prepareStatement(sql);
5、执行sql
int i = prepareStatement.executeUpdate();
6、处理结果
if(i > 0) {
System.out.println("添加成功");
}
7、关闭资源
prepareStatement.close();
connection.close();
参数、方法、返回值类型、返回值
import org.junit.Test;测试@Test
知识总结:
今天学习了eclipse的代码编写,需要结合昨天昨天在SQLyog上创建的数据库建立起连接,今天也是收获满满的一天。
近期评论