[英]Error while establishing connection between java and mysql
Following are my code.......以下是我的代码.......
import java.sql.*;
public class ConnectionTry {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println(1);
Class.forName("com.mysql.jdbc.Driver");
System.out.println(2);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Test","root","password");
if(!conn.isClosed())
{
System.out.println("connected");
}
System.out.println(3);
PreparedStatement ps = conn.prepareStatement("insert into User(user_id,user_name,email,password) values (?,?,?,?);");
System.out.println(4);
ps.setInt(1, 45);
System.out.println(5);
ps.setString(2, "@Suraj");
ps.setString(3, "esuraj@gmail.com");
ps.setString(4, "qwer1234");
int x = ps.executeUpdate();
if(x > 0) {
System.out.println("Registration Successful");
}
else {
System.out.println("Registration not Successful");
}
}
catch(Exception c) {
System.out.println(c.getMessage());
}
}
}
In the above code: Test is database, root is user in mysql, password is mysql password.上面代码中: test是数据库, root是mysql中的用户,密码是mysql密码。
Following error occur when I run the above code.运行上述代码时发生以下错误。
Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
Try and set the character encoding like the error is telling you to do.尝试设置字符编码,就像错误告诉你的那样。
jdbc:mysql://localhost/Test?characterEncoding=utf8
you didnt specify the port on the URL database connection which is by default 3306 and you url should look like this jdbc:mysql://localhost:3306/Test you didnt specify the port on the URL database connection which is by default 3306 and you url should look like this jdbc:mysql://localhost:3306/Test
this core jdbc file
这个核心 jdbc 文件
import java.sql.*;
public class jdbcInsertTable {
public static void insertvalue() throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
Connection con= DriverManager.getConnection
("jdbc:mysql://localhost:3306/oracl","root","root");
System.out.println("Connected database successfully...");
Statement stmt = con.createStatement();
stmt.executeUpdate("insert into employee values('sagar','sales')");
System.out.println("values insert successfully");
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
insertvalue();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.