jdbc 回顾
时间:2021-07-01 10:21:17
帮助过:4人阅读
private static void insertTest()
throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test"
;
Connection con = DriverManager.getConnection(dbURL, "root", "123"
);
Statement st =
con.createStatement();
st.execute("insert into user(ID,NAME) values(1, ‘Zhang San‘)"
);
st.execute("insert into user(ID,NAME) values(2, ‘Li Si‘)"
);
st.execute("insert into user(ID,NAME) values(3, ‘Wang Wu‘)"
);
System.out.println("=====insert test====="
);
showUser(st);
st.close();
con.close();
}
private static void deleteTest()
throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test"
;
Connection con = DriverManager.getConnection(dbURL, "root", "123"
);
Statement st =
con.createStatement();
st.execute("delete from user where ID=3"
);
System.out.println("=====delete test====="
);
showUser(st);
st.close();
con.close();
}
private static void updateTest()
throws SQLException
{
String dbURL = "jdbc:mysql://localhost/test"
;
Connection con = DriverManager.getConnection(dbURL, "root", "123"
);
Statement st =
con.createStatement();
st.executeUpdate("update user set NAME=‘TEST‘ where ID=2"
);
System.out.println("=====update test====="
);
showUser(st);
st.close();
con.close();
}
private static void showUser(Statement st)
throws SQLException
{
ResultSet rs = st.executeQuery("select ID, NAME from user"
);
while(rs.next())
{
int id = rs.getInt("ID"
);
String name = rs.getString("NAME"
);
System.out.println("ID:" + id + "; NAME=" +
name);
}
rs.close();
}
jdbc 回顾
标签:bsp let span into manage div select exception str