我使用vidya
创建了一个名为students
的数据库,其中包含一个名为PhPMyAdmin
的表。我想使用Java代码将值插入数据库。这是代码,但目前MariaDB
JDBC connector
存在问题,如果我的DB_URL
在本地主机上运行,我不确定我的/**
* Created by jalal on 7/6/2016.
*/
import java.sql.*;
import java.util.Calendar;
public class TestPhpMyAdmin
{
public static void main(String[] args)
{
try
{
//static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// create a mysql database connection
String myDriver = "org.mariadb.jdbc";
String DB_URL = "jdbc:mysql://localhost/vidya";
Class.forName(myDriver);
// Database credentials
final String USER = "root";
final String PASS = "newpass";
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// create a sql date object so we can use it in our INSERT statement
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
// the mysql insert statement
String query = " insert into students (ID, last_name, first_name, birthday, hometown)"
+ " values (?, ?, ?, ?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, 808027);
preparedStatement.setString(2, "Davis");
preparedStatement.setString(3, "Felicita");
preparedStatement.setDate(4, startDate);
preparedStatement.setString(5, "Venice");
// execute the preparedstatement
preparedStatement.execute();
conn.close();
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
是否正确。
org.mariadb.jdbc
我收到此错误:
MariaDB
10.1.13-MariaDB
的版本为display: none
答案 0 :(得分:1)
JDBC驱动程序使用DriverManager.registerDriver注册自己。
使用DriverManager.getConnection(...),DriverManager会询问每个驱动程序是否类似于' jdbc:mariadb:..."被处理。如果驱动程序答案为是,则Drivermanager将使用此驱动程序创建连接。
(这将允许例如用于开发环境的H2数据库,以及用于生产的MariaDB数据库,只需更改URL)
MariaDB的驱动程序是" org.mariadb.jdbc.Driver",而不是" org.mariadb.jdbc"这是驱动程序包,但实际上在这种情况下并不有用。可以抑制导致错误的这部分代码:
String myDriver = "org.mariadb.jdbc";
Class.forName(myDriver);
答案 1 :(得分:0)
根据Java IRC频道中的ojacobson
,不需要Class.forName(myDriver);
,因此我对它进行了评论并将值插入到数据库表中:
以下是参考的正确代码:
/**
* Created by jalal on 7/6/2016.
*/
import java.sql.*;
import java.util.Calendar;
public class TestPhpMyAdmin
{
public static void main(String[] args)
{
try
{
// create a mysql database connection
String DB_URL = "jdbc:mysql://localhost/vidya";
// Database credentials
final String USER = "root";
final String PASS = "newpass";
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// create a sql date object so we can use it in our INSERT statement
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
// the mysql insert statement
String query = " insert into students (ID, last_name, first_name, birthday, hometown)"
+ " values (?, ?, ?, ?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, 808027);
preparedStatement.setString(2, "Davis");
preparedStatement.setString(3, "Felicita");
preparedStatement.setDate(4, startDate);
preparedStatement.setString(5, "Venice");
// execute the preparedstatement
preparedStatement.execute();
conn.close();
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
答案 2 :(得分:0)
对于mariadb是:jdbc:mariadb:// localhost /如果您的java版本高于1.6,则不需要classforname ClassForName适用于低于1.6的java。
更改此行:
import java.sql。*;
import java.sql.Date;
java.sql.Date仅对Date进行更改。
不要使用import java.sql。*;