statement添加操作

来源:互联网 发布:电脑学影视制作软件 编辑:程序博客网 时间:2024/06/03 22:48
package mysql;




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;




public class Sql {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
 }

public static Connection getCon() throws SQLException{
Connection con=null;
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123456");

/*用connection获取数据库的连接*/
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return con;
}
public static void main(String[] args) throws SQLException {
String sql="insert into studentmessage(name,xh,pwd,cla,age,籍贯,sex,hobby) values('玫瑰','1201','110','12','125','浙江省','男','玩蛇')";/*创建字符串sql=执行的sql*/
Connection con=getCon();/*获取connection方法*/
try {
Statement stmt=con.createStatement();
int i=stmt.executeUpdate(sql);
System.out.println(i+"行数据受到影响");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
con.close();/*关闭连接*/
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
0 0
原创粉丝点击