import java.sql.*;
Connection con;
String driver =
"com.mysql.jdbc.Driver";
String url =
"jdbc:mysql://127.0.0.1:33/test?useUnicode=true&characterEncoding=UTF-8
String user =
"root";
String password =
"root";
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println(
"Succeeded connecting to the Database!");
Statement statement = con.createStatement();
String sql =
"insert into weather(address,temperature,weatherstatus,date) values(?,?,?,?) ";
PreparedStatement ps=null;
ps=con.prepareStatement(sql);
ps.setString(
1,address);
ps.setString(
2,temperature);
ps.setString(
3,weather);
ps.setString(
4,date);
con.close();
con.close();
} catch(ClassNotFoundException e) {
System.out.println(
"Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println(
"数据库数据成功获取!!");
}
}
String url = “jdbc:mysql://127.0.0.1:33/test?useUnicode=true&characterEncoding=UTF-8 可以解决中文写入数据库乱码问题。建表时把编码方式设置为utf_8