创建表
create table Employee(
id int primary key,
name varchar(20),
age int
);
import java.sql;
public class test{
public static void main(String[] args)throws Exception{
String user = "user1";
String password = "pwd1";
String url = "jdbc:mysql://localhost:3306/Test";
String driver = "com.mysql.jdbc.Driver";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
stmt = con.createStatement();
stmt.execute("insert into Employee values(1,'James1',25)");
stmt.execute("insert into Employee values(2,'James2',26)");
rs = stmt.executeQuery("select * from Employee");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(s)+" "+rs.getInt(3));
}
}
catch(SQLException e1){
e1.printStack();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
}
JDBC访问mysql
猜你喜欢
转载自blog.csdn.net/v2020877/article/details/82315564
今日推荐
周排行