8:30-9:00 学习打卡
9:30-12:00 看视频
15:00-17:00 写qq项目
jdbc的应用
1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
2.获取连接对象
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
3.获取执行sql语句对象
Statement st=con.createStatement();
4.定义sql
String s="INSERT INTO sutent ( id, NAME ) VALUES ( 1, 'jjj' );";
5.执行sql语句
st.executeUpdate(s1);
6.释放资源
con.close();
st.close();
package qqserver.mode;
import java.sql.*;
//mysql的连接
public class Mysql {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/test";
private String user=null;
private String pass=null;
private String sql="mysql.ini";
public Mysql(String user,String pass) {
this.user=user;
this.pass=pass;
}
public static void main(String[] args) {
Mysql mq=new Mysql("root","123456");
Connection con=null;
Statement st=null;
try {
Class.forName(driver);//注册驱动
con=DriverManager.getConnection(url);//获取连接
st=con.createStatement();
String s1="INSERT INTO sutent ( id, NAME ) VALUES ( 1, 'jjj' );"; //插入id为1 名字为jjj进入表sutent
String s2="SELECT id FROM sutent;"; //查看表sutent所有的id元素
int i1=st.executeUpdate(s1);
System.out.println("修改了"+i1+"行");
ResultSet i2=st.executeQuery(s2);
while(i2.next()){
i2.getInt(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(con!=null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if(st!=null)
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}